API reference¶
aiohttp_asgi¶
- class aiohttp_asgi.ASGIResource(app, root_path='/', name=None)[source]¶
Bases:
AbstractResourceAn aiohttp resource which serves an ASGI application.
Register it in an aiohttp router, and the ASGI application answers every request which starts with root_path.
resource = ASGIResource(asgi_app, root_path="/api") aiohttp_app.router.register_resource(resource) resource.lifespan_mount(aiohttp_app)
- Parameters:
app (
Callable[[MutableMapping[str,Any],Callable[[],Awaitable[MutableMapping[str,Any]]],Callable[[MutableMapping[str,Any]],Any]],Coroutine[Any,Any,Any]]) – the ASGI application.root_path (
str) – the path prefix of the application.name (
Optional[str]) – the resource name for the aiohttp router.
- SHUTDOWN_TIMEOUT = 60¶
Maximum time in seconds to wait for the lifespan task after shutdown.
- url_for(**kwargs)[source]¶
Construct url for resource with additional params.
- Parameters:
kwargs (
str)- Return type:
URL
- get_info()[source]¶
Return a dict with additional info useful for introspection
- Return type:
ResourceInfoDict
- async resolve(request)[source]¶
Resolve resource.
Return (UrlMappingMatchInfo, allowed_methods) pair.
- lifespan_mount(app)[source]¶
Send the ASGI lifespan events with the aiohttp application.
The method appends a cleanup context to the aiohttp application. The context sends lifespan.startup on startup, and lifespan.shutdown on shutdown. Without this call the ASGI application gets no lifespan events.
- Parameters:
app (
Application) – the aiohttp application which gives the events.- Return type:
aiohttp_asgi.resource¶
- class aiohttp_asgi.resource.ASGIMatchInfo(handler)[source]¶
Bases:
AbstractMatchInfoThe match info which ASGIResource.resolve() returns.
aiohttp takes the handler, the application stack and the expect handler from this object.
- property expect_handler: Callable[[Request], Awaitable[Optional[StreamResponse]]]¶
Expect handler for 100-continue processing
- property http_exception: Optional[HTTPException]¶
HTTPException instance raised on router’s resolving, or None
- property apps: Tuple[Application, ...]¶
Stack of nested applications.
Top level application is left-most element.
- add_app(app)[source]¶
Add application to the nested apps stack.
- Parameters:
app (
Application)- Return type:
- class aiohttp_asgi.resource.ASGIContext(app, request, root_path)[source]¶
Bases:
objectThe bridge between one aiohttp request and the ASGI application.
The context builds the ASGI scope, moves the body in both directions and makes the aiohttp response.
Type aliases¶
aiohttp_asgi.resource gives the type aliases of the ASGI protocol:
Alias |
Meaning |
|---|---|
|
The scope: a mutable mapping with string keys |
|
The |
|
The |
|
The application: a callable which takes the scope, |
aiohttp_asgi.main¶
The command line server. The functions are useful for a custom entry point which needs the same behaviour.
- aiohttp_asgi.__main__.parse_app_spec(spec)[source]¶
Check the “module_name:asgi_application” format.
This function does not import the module, because the import must happen in the worker process, after the fork.
- aiohttp_asgi.__main__.load_app(spec)[source]¶
Import the ASGI application from the “module_name:asgi_application” spec.
Call this in the worker process. An application can start threads or event loops on import, and the fork does not copy them correctly.
- aiohttp_asgi.__main__.new_event_loop(implementation='stdlib')[source]¶
Create the event loop for the server.
Call this in the worker process so optional loops are imported and created after the fork.
- Parameters:
implementation (
str)- Return type:
- aiohttp_asgi.__main__.stop_on_signal(stop)[source]¶
Set the stop event on SIGINT and SIGTERM.
The server stops from a signal callback, because an exception from a signal handler does not leave every event loop implementation, and in a worker it can also die inside the request handler task.
- async aiohttp_asgi.__main__.run_server(app, sock)[source]¶
Serve the application on the bound socket until a signal arrives.
- Parameters:
app (
Application)sock (
socket)
- Return type: