Lifespan
ASGI lifespan event management.
ASGI Lifespan Management.
Purpose
ServerLifespan handles the ASGI lifespan protocol for AsgiServer, managing startup/shutdown sequences for mounted applications.
Features: - Full ASGI lifespan protocol support - Startup and shutdown handlers for mounted apps - Support for sync and async handlers on sub-apps - Error handling with proper ASGI messages
Definition:
class ServerLifespan:
__slots__ = ("server",)
def __init__(self, server: AsgiServer)
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None
async def startup(self) -> None
async def shutdown(self) -> None
Example:
from genro_asgi import AsgiServer
server = AsgiServer()
# ServerLifespan is created automatically
# server.lifespan handles all lifespan events
Design Notes
ServerLifespan holds reference to server as self.server
Sub-apps can define on_startup/on_shutdown methods (sync or async)
Errors during startup send lifespan.startup.failed
Errors during shutdown are logged but don’t prevent completion
- genro_asgi.lifespan.Lifespan
alias of
ServerLifespan
- class genro_asgi.lifespan.ServerLifespan(server)[source]
Bases:
objectASGI Lifespan handler for AsgiServer.
Manages the lifespan protocol, coordinating startup and shutdown of all mounted applications.
- server
The AsgiServer instance this lifespan manages.
Example
>>> # Automatically created by AsgiServer >>> server = AsgiServer() >>> # server.lifespan is a ServerLifespan instance
- __init__(server)[source]
Initialize ServerLifespan.
- Parameters:
server (
AsgiServer) – The AsgiServer instance to manage.
- server