import contextlib import time import threading import uvicorn class Server(uvicorn.Server): @contextlib.contextmanager def run_in_thread(self): thread = threading.Thread( target=self.run, ) thread.start() try: while not self.started: time.sleep(1e-3) yield finally: self.should_exit = True thread.join()