index.txt 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. hypercorn
  15. uvicorn
  16. The ``application`` object
  17. ==========================
  18. Like WSGI, ASGI has you supply an ``application`` callable which
  19. the application server uses to communicate with your code. It's commonly
  20. provided as an object named ``application`` in a Python module accessible to
  21. the server.
  22. The :djadmin:`startproject` command creates a file
  23. :file:`<project_name>/asgi.py` that contains such an ``application`` callable.
  24. It's not used by the development server (``runserver``), but can be used by
  25. any ASGI server either in development or in production.
  26. ASGI servers usually take the path to the application callable as a string;
  27. for most Django projects, this will look like ``myproject.asgi:application``.
  28. .. warning::
  29. While Django's default ASGI handler will run all your code in a synchronous
  30. thread, if you choose to run your own async handler you must be aware of
  31. async-safety.
  32. Do not call blocking synchronous functions or libraries in any async code.
  33. Django prevents you from doing this with the parts of Django that are not
  34. async-safe, but the same may not be true of third-party apps or Python
  35. libraries.
  36. Configuring the settings module
  37. ===============================
  38. When the ASGI server loads your application, Django needs to import the
  39. settings module — that's where your entire application is defined.
  40. Django uses the :envvar:`DJANGO_SETTINGS_MODULE` environment variable to locate
  41. the appropriate settings module. It must contain the dotted path to the
  42. settings module. You can use a different value for development and production;
  43. it all depends on how you organize your settings.
  44. If this variable isn't set, the default :file:`asgi.py` sets it to
  45. ``mysite.settings``, where ``mysite`` is the name of your project.
  46. Applying ASGI middleware
  47. ========================
  48. To apply ASGI middleware, or to embed Django in another ASGI application, you
  49. can wrap Django's ``application`` object in the ``asgi.py`` file. For example::
  50. from some_asgi_library import AmazingMiddleware
  51. application = AmazingMiddleware(application)