ushka.core.server
The production server: Uvicorn with an idle loop that does not burn CPU.
Stock Uvicorn wakes every worker 10 times a second (main_loop sleeps
0.1 s) and, with several workers, wakes the supervisor twice a second to ping
each worker through a pipe. An idle app then costs about 1 s of CPU per minute.
Here:
- the worker ticks once a second (that is all the
Dateheader needs); - the supervisor sleeps until a worker dies (its process sentinel), a signal
arrives, or the next health check, every
HEALTHCHECK_EVERYseconds; - a worker that is still starting (importing the app) is judged by its process
being alive, not by the ping, for
STARTUP_GRACEseconds, so a slow start is no longer mistaken for a hang and killed in a loop.
This module imports Uvicorn, so only ushka run imports it.
In this module
class QuietServer | A Uvicorn server that wakes once a second instead of ten times. |
class Worker | A worker process that runs a QuietServer and remembers when it started. |
class Supervisor | Keeps the workers alive without polling them. |
def run_server() | Serves app (module:attribute) until stopped. Several workers get a Supervisor. |
class QuietServer (Server)
A Uvicorn server that wakes once a second instead of ten times.
main_loop
async def main_loop() -> Noneclass Worker (Process)
A worker process that runs a QuietServer and remembers when it started.
start
def start() -> Nonestarting
def starting() -> boolclass Supervisor (Multiprocess)
Keeps the workers alive without polling them.
run
def run() -> Nonekeep_subprocess_alive
def keep_subprocess_alive(check_health: bool = True) -> Nonedef run_server
def run_server(
app: str,
*,
host: str,
port: int,
workers: int,
log_config: dict,
) -> NoneServes app (module:attribute) until stopped. Several workers get a Supervisor.