1. ==============================
    
  2. How to use Django with Uvicorn
    
  3. ==============================
    
  4. 
    
  5. .. highlight:: bash
    
  6. 
    
  7. Uvicorn_ is an ASGI server based on ``uvloop`` and ``httptools``, with an
    
  8. emphasis on speed.
    
  9. 
    
  10. Installing Uvicorn
    
  11. ==================
    
  12. 
    
  13. You can install Uvicorn with ``pip``::
    
  14. 
    
  15.     python -m pip install uvicorn
    
  16. 
    
  17. Running Django in Uvicorn
    
  18. =========================
    
  19. 
    
  20. When Uvicorn is installed, a ``uvicorn`` command is available which runs ASGI
    
  21. applications. Uvicorn needs to be called with the location of a module
    
  22. containing an ASGI application object, followed by what the application is
    
  23. called (separated by a colon).
    
  24. 
    
  25. For a typical Django project, invoking Uvicorn would look like::
    
  26. 
    
  27.     python -m uvicorn myproject.asgi:application
    
  28. 
    
  29. This will start one process listening on ``127.0.0.1:8000``. It requires that
    
  30. your project be on the Python path; to ensure that run this command from the
    
  31. same directory as your ``manage.py`` file.
    
  32. 
    
  33. In development mode, you can add ``--reload`` to cause the server to reload any
    
  34. time a file is changed on disk.
    
  35. 
    
  36. For more advanced usage, please read the `Uvicorn documentation <Uvicorn_>`_.
    
  37. 
    
  38. Deploying Django using Uvicorn and Gunicorn
    
  39. ===========================================
    
  40. 
    
  41. Gunicorn_ is a robust web server that implements process monitoring and automatic
    
  42. restarts. This can be useful when running Uvicorn in a production environment.
    
  43. 
    
  44. To install Uvicorn and Gunicorn, use the following::
    
  45. 
    
  46.     python -m pip install uvicorn gunicorn
    
  47. 
    
  48. Then start Gunicorn using the Uvicorn worker class like this::
    
  49. 
    
  50.     python -m gunicorn myproject.asgi:application -k uvicorn.workers.UvicornWorker
    
  51. 
    
  52. .. _Uvicorn: https://www.uvicorn.org/
    
  53. .. _Gunicorn: https://gunicorn.org/