May 14, 2021 Django
In addition to WSGI, Django supports deployment on ASGI, an emerging Python standard for asynchronous Web servers and applications.
Django's startproject management command sets you a default ASGI configuration that you can adapt to the needs of your project and guide any ASGI-compliant application server.
Django includes the following getting started documentation for ASGI servers:
Like WSGI, ASGI provides you with an application callable object that the application server uses to communicate with your code. Usually the application is provided as an object named in the Python module that is accessible to the server.
The startproject command creates a /asgi.py file that contains such an application callable file.
The development server (runserver) does not use it, but any ASGI server can use it in development or production.
ASGI servers typically use callable application paths as strings. For most Django projects, it looks like myproject.asgi:application.
Warning: Although Django's default ASGI handler will run all code in the synchronization thread, you must be aware of asynchronous security if you choose to run your own asynchronous handler.
Do not call blocking synchronization functions or libraries in any asynchronous code. Django prohibits you from doing this using the non-asynchronous security Django section, but this may not be the case with third-party applications or Python libraries.
When the ASGI server loads your application, Django needs to import the settings module - defining the location of the entire application.
Django uses DJANGO_SETTINGS_MODULE environment variables to find the appropriate setup module. I t must contain a dashed path to the settings module. Y ou can use different values for development and production. It all depends on how you organize your settings.
If this variable is not set, the default asgi.py set it to mysite.settings, where mysite is the name of the project.
To apply ANSGI middleware, or embed Django in another ASGI application, you can wrap Django's application objects in asgi.py file. For example:
from some_asgi_library import AmazingMiddleware
application = AmazingMiddleware(application)
Daphne is a pure Python ASGI server for UNIX, maintained by members of the Django project. It acts as a reference server for ASGI.
You can install Daphne pip using the following commands:
python -m pip install daphne
After Daphne is installed, Daphne is provided with a command to start the Daphne server process. At its simplest, Daphne needs to be called using the location of the module that contains the ASGI application object, followed by the location where the application is called (separated by a colon).
For a typical Django project, call Daphne as follows:
daphne myproject.asgi:application
This will start listening to a process 127.0.0.1:8000. It requires your project to be on the manage.py Python path;
Uvicorn is based on uvloop and the ASGI server httptools, focusing on speed.
You can install Ucicorn pip using the following commands:
python -m pip install uvicorn
After Uvicorn is installed, Ucicorn is provided with commands to run the ASGI application. Ucicorn must be called using the location of the module that contains the ASGI application object, and then the application (separated by a colon).
For a typical Django project, call Ucicorn as follows:
uvicorn myproject.asgi:application
This will start listening to a process 127.0.0.1:8000. It requires your project to be on the manage.py Python path;
For more advanced usage, read the Ucicorn documentation.
For more information:
https://docs.djangoproject.com/en/3.0/