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
ASGIHandlersubclass 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
ClientSessionor an HTTPXAsyncClientwhen your app starts, and close them properly when your app ends. This concept is similar to events in FastAPI.
Quickstart¶
-
Requirements: Python
>=3.10, <3.14and Django>=4.2,<6(excluding5.0.0 .. 5.0.2). Install the package:or
or
-
Modify
asgi.pyto 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: