GitHub

API reference for AsgiApp.

An ASGI 3 application (await app(scope, receive, send)) serving an HTTP app.

runtime holds the layer's services: built by the lifespan scope's startup, or by the first request when the server sends none, and released at its shutdown, which interrupts requests in flight (a plain app is uninterruptible, as in Effect, so it is awaited; router routes are interruptible). Each request runs on its own fiber, on the server's loop.

Attributes

AttributeTypeDescription
runtime= runtime

Functions

__init__

__init__(app, runtime, middleware) -> None
ParameterTypeDescription
appEffect[HttpServerResponse, Any, Any]
runtimeManagedRuntime[Any, Any]
middlewareHttpMiddleware | None
def __init__(
    self,
    app: Effect[HttpServerResponse, Any, Any],
    runtime: ManagedRuntime[Any, Any],
    middleware: HttpMiddleware | None,
) -> None:
    self.runtime = runtime
    self._handler = AsgiHandler(app, middleware, Context.empty(), self._fork)

__call__

__call__(scope, receive, send) -> None
ParameterTypeDescription
scopeScope
receiveReceive
sendSend
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
    if scope["type"] == "lifespan":
        await self._lifespan(receive, send)
    else:
        await self._handler(scope, receive, send)

lifespan

lifespan() -> AsyncGenerator[None]

Build the services on enter and release them on exit: the lifespan of a host app this one is mounted in (FastAPI(lifespan= lambda _: app.lifespan())), whose server sends no lifespan to mounted apps.

Returns AsyncGenerator[None]

@asynccontextmanager
async def lifespan(self) -> AsyncGenerator[None]:
    """Build the services on enter and release them on exit: the
    lifespan of a host app this one is mounted in (``FastAPI(lifespan=
    lambda _: app.lifespan())``), whose server sends no lifespan to
    mounted apps."""
    await self.runtime.context()
    try:
        yield
    finally:
        await self.runtime.dispose_async()