index.txt 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. =======================
  2. How to deploy with ASGI
  3. =======================
  4. As well as WSGI, Django also supports deploying on ASGI_, the emerging Python
  5. standard for asynchronous web servers and applications.
  6. .. _ASGI: https://asgi.readthedocs.io/en/latest/
  7. Django's :djadmin:`startproject` management command sets up a default ASGI
  8. configuration for you, which you can tweak as needed for your project, and
  9. direct any ASGI-compliant application server to use.
  10. Django includes getting-started documentation for the following ASGI servers:
  11. .. toctree::
  12. :maxdepth: 1
  13. daphne
  14. uvicorn
  15. The ``application`` object
  16. ==========================
  17. Like WSGI, ASGI has you supply an ``application`` callable which
  18. the application server uses to communicate with your code. It's commonly
  19. provided as an object named ``application`` in a Python module accessible to
  20. the server.
  21. The :djadmin:`startproject` command creates a file
  22. :file:`<project_name>/asgi.py` that contains such an ``application`` callable.
  23. It's not used by the development server (``runserver``), but can be used by
  24. any ASGI server either in development or in production.
  25. ASGI servers usually take the path to the application callable as a string;
  26. for most Django projects, this will look like ``myproject.asgi:application``.
  27. .. warning::
  28. While Django's default ASGI handler will run all your code in a synchronous
  29. thread, if you choose to run your own async handler you must be aware of
  30. async-safety.
  31. Do not call blocking synchronous functions or libraries in any async code.
  32. Django prevents you from doing this with the parts of Django that are not
  33. async-safe, but the same may not be true of third-party apps or Python
  34. libraries.
  35. Configuring the settings module
  36. ===============================
  37. When the ASGI server loads your application, Django needs to import the
  38. settings module — that's where your entire application is defined.
  39. Django uses the :envvar:`DJANGO_SETTINGS_MODULE` environment variable to locate
  40. the appropriate settings module. It must contain the dotted path to the
  41. settings module. You can use a different value for development and production;
  42. it all depends on how you organize your settings.
  43. If this variable isn't set, the default :file:`asgi.py` sets it to
  44. ``mysite.settings``, where ``mysite`` is the name of your project.
  45. Applying ASGI middleware
  46. ========================
  47. To apply ASGI middleware, or to embed Django in another ASGI application, you
  48. can wrap Django's ``application`` object in the ``asgi.py`` file. For example::
  49. from some_asgi_library import AmazingMiddleware
  50. application = AmazingMiddleware(application)