24 lines
623 B
Python
24 lines
623 B
Python
from uvicorn import Config, Server
|
|
from threading import Thread
|
|
import asyncio
|
|
|
|
|
|
class ThreadedUvicorn:
|
|
def __init__(self, config: Config):
|
|
self.server = Server(config)
|
|
self.thread = Thread(daemon=True, target=self.server.run)
|
|
|
|
def start(self):
|
|
self.thread.start()
|
|
asyncio.run(self.wait_for_started())
|
|
|
|
async def wait_for_started(self):
|
|
while not self.server.started:
|
|
await asyncio.sleep(0.1)
|
|
|
|
def stop(self):
|
|
if self.thread.is_alive():
|
|
self.server.should_exit = True
|
|
while self.thread.is_alive():
|
|
continue
|