State manager (preferred)
How does it work¶
When you execute the Django project using the ASGI server (like uvicorn), it sends lifespan events at its startup. These lifespan events are ignored by the standard Django ASGI handler. The lifespan events include the lifespan scope state, which is a Python dictionary that is preserved by the ASGI server and allowed to be modified by the developer. Hence, it's a suitable location for storing global application states or shared objects. For instance, one could create a shared HTTPX async client to implement connection pooling.
Accessing the shared state¶
To access the shared state within a view, your project must include middleware. This middleware assigns a new attribute to the request object, using the state it receives from the ASGI server.
Context manager¶
You must define an async context manager. The code for this can be placed anywhere; in this example, the function is defined in the context.py
file. The code up to the yield statement is executed when the ASGI server starts, and the remaining part is executed when the server shuts down.
context.py | |
---|---|
Registering the context manager¶
The manager that you have just defined needs to be registered. The most suitable location for registration is Django AppConfig.
apps.py | |
---|---|
View¶
After the above steps, you can access the shared state in the views.