2020-12-24 05:55:19 +00:00
|
|
|
import contextlib
|
|
|
|
import threading
|
2020-12-25 19:07:12 +00:00
|
|
|
import time
|
|
|
|
|
2020-12-24 05:55:19 +00:00
|
|
|
import uvicorn
|
|
|
|
|
|
|
|
|
|
|
|
class UvicornServer(uvicorn.Server):
|
|
|
|
"""
|
|
|
|
Multithreaded server - as found in https://github.com/encode/uvicorn/issues/742
|
|
|
|
"""
|
|
|
|
def install_signal_handlers(self):
|
2020-12-27 09:56:19 +00:00
|
|
|
"""
|
|
|
|
In the parent implementation, this starts the thread, therefore we must patch it away here.
|
|
|
|
"""
|
2020-12-24 05:55:19 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def run_in_thread(self):
|
|
|
|
self.thread = threading.Thread(target=self.run)
|
|
|
|
self.thread.start()
|
|
|
|
while not self.started:
|
|
|
|
time.sleep(1e-3)
|
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
self.should_exit = True
|
|
|
|
self.thread.join()
|