Classes
_WorkerHandle
1
▼
Metadata about a running worker (thread or process).
_WorkerHandle
1
▼
Metadata about a running worker (thread or process).
Methods
Internal Methods 1 ▼
__init__
2
▼
__init__
2
▼
def __init__(self, worker_id: int, target: threading.Thread | multiprocessing.Process) -> None
Parameters
| Name | Type | Description |
|---|---|---|
worker_id |
— |
|
target |
— |
Supervisor
12
▼
Spawn and supervise N workers as threads or processes.
The supervisor detects the GIL state and pi…
Supervisor
12
▼
Spawn and supervise N workers as threads or processes.
The supervisor detects the GIL state and picks the appropriate
spawning strategy automatically. Workers share the frozen
ServerConfigand (in thread mode) the ASGI app reference.
Methods
mode
0
WorkerMode
▼
The active worker mode (``"thread"`` or ``"process"``).
property
mode
0
WorkerMode
▼
def mode(self) -> WorkerMode
Returns
WorkerMode
worker_count
0
int
▼
Number of workers the supervisor manages.
property
worker_count
0
int
▼
def worker_count(self) -> int
Returns
int
run
1
▼
Start all workers and block until shutdown.
Installs signal handlers, spawns w…
run
1
▼
def run(self, sockets: list[socket.socket]) -> None
Start all workers and block until shutdown.
Installs signal handlers, spawns workers, runs the health-check loop, then joins all workers on shutdown.
Parameters
| Name | Type | Description |
|---|---|---|
sockets |
— |
One socket per worker, created by |
shutdown
0
▼
Signal all workers to stop (non-blocking).
shutdown
0
▼
def shutdown(self) -> None
restart_workers
0
▼
Gracefully restart all workers (for dev reload).
Signals all running workers t…
restart_workers
0
▼
def restart_workers(self) -> None
Gracefully restart all workers (for dev reload).
Signals all running workers to stop, waits for them to drain, clears the shutdown event, and spawns fresh workers.
When anapp_pathwas provided and workers run as threads,
the app module is reimported so that code changes on disk take
effect. Process-based workers get fresh imports automatically
on fork and don't need explicit reimport.
Internal Methods 7 ▼
__init__
6
▼
__init__
6
▼
def __init__(self, config: ServerConfig, app: ASGIApp, *, mode: WorkerMode | None = None, ssl_context: ssl.SSLContext | None = None, lifecycle_collector: LifecycleCollector | None = None, app_path: str | None = None) -> None
Parameters
| Name | Type | Description |
|---|---|---|
config |
— |
|
app |
— |
|
mode |
— |
Default:None
|
ssl_context |
— |
Default:None
|
lifecycle_collector |
— |
Default:None
|
app_path |
— |
Default:None
|
_spawn_worker
1
▼
Create and start a single worker.
_spawn_worker
1
▼
def _spawn_worker(self, worker_id: int) -> None
Parameters
| Name | Type | Description |
|---|---|---|
worker_id |
— |
_respawn_worker
1
▼
Restart a crashed worker if within restart budget.
_respawn_worker
1
▼
def _respawn_worker(self, worker_id: int) -> None
Parameters
| Name | Type | Description |
|---|---|---|
worker_id |
— |
_watch
0
▼
Health-check loop — detects crashed workers and restarts them.
_watch
0
▼
def _watch(self) -> None
_drain
0
▼
Wait for all workers to finish, then clean up.
_drain
0
▼
def _drain(self) -> None
_force_stop
1
▼
Force-terminate a worker that did not drain in time.
_force_stop
1
▼
def _force_stop(self, handle: _WorkerHandle) -> None
Parameters
| Name | Type | Description |
|---|---|---|
handle |
— |
_install_signals
0
▼
Install SIGINT/SIGTERM handlers to trigger graceful shutdown.
Only effective w…
_install_signals
0
▼
def _install_signals(self) -> None
Install SIGINT/SIGTERM handlers to trigger graceful shutdown.
Only effective when the supervisor runs on the main thread (e.g.,
direct testing). In production the supervisor runs inside a
run_in_executor thread, so signal.signal()will fail
silently.Serverinstalls asyncio signal handlers that call
supervisor.shutdown()instead.
Functions
_target_id
1
str
▼
Return an identifier string for a thread or process.
_target_id
1
str
▼
def _target_id(target: threading.Thread | multiprocessing.Process) -> str
Parameters
| Name | Type | Description |
|---|---|---|
target |
threading.Thread | multiprocessing.Process |
Returns
str