GitHub

AsgiHandler

API reference for AsgiHandler.

An ASGI app answering http and websocket scopes with app: fork starts each request's fiber with context and the request.

Functions

__init__

__init__(app, middleware, context, fork) -> None
ParameterTypeDescription
appEffect[HttpServerResponse, Any, Any]
middlewareHttpMiddleware | None
contextContext[Any]
forkFork
def __init__(
    self,
    app: Effect[HttpServerResponse, Any, Any],
    middleware: HttpMiddleware | None,
    context: Context[Any],
    fork: Fork,
) -> None:
    self._app = app
    self._middleware = middleware
    self._context = context
    self._fork = fork
    self._web = to_web_handler_with(context, fork=fork)(app, middleware)
    self._open: set[AbortController] = set()

end_responses

end_responses() -> None

Abort every http request in flight, on the server's loop: a handler still running is interrupted, and a streamed body ends where it is. A server calls it once graceful_shutdown_timeout has passed, so an endless body (SSE) cannot hold its shutdown open.

def end_responses(self) -> None:
    """Abort every ``http`` request in flight, on the server's loop: a
    handler still running is interrupted, and a streamed body ends where
    it is. A server calls it once ``graceful_shutdown_timeout`` has
    passed, so an endless body (SSE) cannot hold its shutdown open."""
    for controller in tuple(self._open):
        controller.abort()

__call__

__call__(scope, receive, send) -> None
ParameterTypeDescription
scopeScope
receiveReceive
sendSend
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
    match scope["type"]:
        case "http":
            controller = AbortController()
            self._open.add(controller)
            try:
                await serve_http(self._web, scope, receive, send, controller)
            finally:
                self._open.discard(controller)
        case "websocket":
            await serve_websocket(
                scope,
                receive,
                send,
                app=self._app,
                middleware=self._middleware,
                context=self._context,
                fork=self._fork,
            )
        case other:
            raise UnsupportedScopeError(f"unsupported ASGI scope type: {other!r}")