Module

asgi.lifespan

ASGI lifespan handler.

Manages the ASGI lifespan protocol — sends startup/shutdown events to the application and waits for completion. Used as an async context manager so the server can bracket its run loop with lifespan events.

Handles apps that don't support lifespan — whether they raise an exception or silently return for non-HTTP scopes.

Functions

run_lifespan 2 AsyncIterator[None]
Run the ASGI lifespan protocol as an async context manager. Sends lifespan.sta…
async
async def run_lifespan(app: ASGIApp, config: ServerConfig) -> AsyncIterator[None]

Run the ASGI lifespan protocol as an async context manager.

Sends lifespan.startup on entry, waits for the app to respond with lifespan.startup.complete, yields control to the caller, then sends lifespan.shutdown on exit.

If the app doesn't support lifespan (raises an exception or returns silently during startup), the lifespan is treated as a no-op.

Parameters
Name Type Description
app ASGIApp

The ASGI application.

config ServerConfig

Server configuration.

Returns
AsyncIterator[None]