Django ASGI Handler with Lifespan protocol support
- Documentation: https://illagrenan.github.io/django-asgi-lifespan
- PyPI: https://pypi.org/project/django-asgi-lifespan/
- License: MIT
Main features¶
- The package includes a Django
ASGIHandler
subclass that handles the ASGI Lifespan Protocol without affecting HTTP request handling. - Startup and Shutdown Lifespan events are converted to Django signals.
- The package allows for awaiting on signal receivers. This means you can set up things like an aiohttp
ClientSession
or an HTTPXAsyncClient
when your app starts, and close them properly when your app ends. This concept is similar to events in FastAPI.
Quickstart¶
-
Python
^3.10 || ^3.11 || ^3.12 || ^3.13
and Django^4.2 || ^5.0.3 || ^5.1
are supported. To install this package, run:or
-
Modify
asgi.py
to use a ASGI Lifespan compatible handler. -
Add state middleware:
4. Register async context manager:context.py apps.py -
Use some resource (in this case the HTTPX client) in views.
-
Run uvicorn: