switch to internally managed uvicorn

Signed-off-by: Vladimir Mandic <mandic00@live.com>
This commit is contained in:
Vladimir Mandic
2026-08-06 13:24:48 +02:00
parent 0efd18aed5
commit 6e448de56c
4 changed files with 91 additions and 14 deletions
+33 -11
View File
@@ -1,36 +1,59 @@
import threading
import logging
import time
import asyncio
import uvicorn
import fastapi
from modules.logger import log
class UvicornServer(uvicorn.Server):
def __init__(self, app: fastapi.FastAPI, listen = None, port = None, keyfile = None, certfile = None, loop = "auto", http = "auto"):
def __init__(self, app: fastapi.FastAPI, host = None, listen = None, port = None, keyfile = None, certfile = None, loop = "auto", http = "auto"):
self.app: fastapi.FastAPI = app
self.thread: threading.Thread = None
self.loop = None
self.wants_restart = False
self.should_exit = False
kwargs = {
'loop': loop, # auto, asyncio, uvloop
'http': http, # auto, h11, httptools
'interface': "auto", # auto, asgi3, asgi2, wsgi
'ws': "auto", # auto, websockets, wsproto, websockets-sansio
'timeout_keep_alive': 60, # default=5
'ws_max_size': 1024 * 1024 * 1024, # default 16MB
'ws_max_queue': 64, # default=32
'ws_ping_interval': 30, # default=20
'ws_ping_timeout': 60, # default=20
'timeout_graceful_shutdown': 5, # default=None
'access_log': False, # default=True
'server_header': False, # default=True
'date_header': False, # default=True
'backlog': 4096, # default=2048
'reload': False, # default=False
}
self.config = uvicorn.Config(
app=self.app,
host = "0.0.0.0" if listen else "127.0.0.1",
port = port or 7861,
loop = loop, # auto, asyncio, uvloop
http = http, # auto, h11, httptools
interface = "auto", # auto, asgi3, asgi2, wsgi
ws = "auto", # auto, websockets, wsproto
host = host or ("0.0.0.0" if listen else "127.0.0.1"),
port = port or 7860,
log_level = logging.WARNING,
backlog = 4096, # default=2048
timeout_keep_alive = 60, # default=5
ssl_keyfile = keyfile,
ssl_certfile = certfile,
ws_max_size = 1024 * 1024 * 1024, # default 16MB
**kwargs
)
super().__init__(config=self.config)
log.info(f'Server: uvicorn={kwargs}')
def start(self):
self.thread = threading.Thread(target=self.run, daemon=True)
self.wants_restart = False
self.thread.start()
start = time.time()
while not self.started:
time.sleep(1e-3)
if time.time() - start > 5:
raise RuntimeError("Server failed to start. Please check that the port is available.")
policy = asyncio.get_event_loop_policy()
self.loop = f"{type(policy).__module__}.{type(policy).__name__}"
def stop(self):
self.should_exit = True
@@ -71,7 +94,6 @@ class HypercornServer:
self.thread = threading.Thread(target=self.run, daemon=True)
self.thread.start()
elif self.loop == 'asyncio': # does not run in thread
import asyncio
from hypercorn.asyncio import serve
self.server = serve(self.app, self.config)
asyncio.run(self.server)