Ushka

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 Date header needs);
  • the supervisor sleeps until a worker dies (its process sentinel), a signal arrives, or the next health check, every HEALTHCHECK_EVERY seconds;
  • a worker that is still starting (importing the app) is judged by its process being alive, not by the ping, for STARTUP_GRACE seconds, 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 QuietServerA Uvicorn server that wakes once a second instead of ten times.
class WorkerA worker process that runs a QuietServer and remembers when it started.
class SupervisorKeeps 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() -> None

class Worker (Process)

A worker process that runs a QuietServer and remembers when it started.

start

def start() -> None

starting

def starting() -> bool

class Supervisor (Multiprocess)

Keeps the workers alive without polling them.

run

def run() -> None

keep_subprocess_alive

def keep_subprocess_alive(check_health: bool = True) -> None

def run_server

def run_server(
    app: str,
    *,
    host: str,
    port: int,
    workers: int,
    log_config: dict,
) -> None

Serves app (module:attribute) until stopped. Several workers get a Supervisor.