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: object

ASGI 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
async shutdown()[source]

Execute shutdown sequence.

Calls on_shutdown on all mounted apps in reverse order. Apps can define on_shutdown as sync or async method. Errors are logged but don’t prevent other apps from shutting down.

Return type:

None

async startup()[source]

Execute startup sequence.

Calls on_startup on all mounted apps. Apps can define on_startup as sync or async method.

Return type:

None