2019-04-12 13:15:18 +00:00
|
|
|
==============================
|
|
|
|
How to use Django with Uvicorn
|
|
|
|
==============================
|
|
|
|
|
|
|
|
Uvicorn_ is an ASGI server based on ``uvloop`` and ``httptools``, with an
|
|
|
|
emphasis on speed.
|
|
|
|
|
|
|
|
Installing Uvicorn
|
|
|
|
==================
|
|
|
|
|
|
|
|
You can install Uvicorn with ``pip``:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2019-04-12 13:15:18 +00:00
|
|
|
.. code-block:: shell
|
|
|
|
|
2022-10-26 09:51:15 +00:00
|
|
|
python -m pip install uvicorn
|
2019-04-12 13:15:18 +00:00
|
|
|
|
|
|
|
Running Django in Uvicorn
|
|
|
|
=========================
|
|
|
|
|
|
|
|
When Uvicorn is installed, a ``uvicorn`` command is available which runs ASGI
|
|
|
|
applications. Uvicorn needs to be called with the location of a module
|
2019-09-11 10:55:17 +00:00
|
|
|
containing an ASGI application object, followed by what the application is
|
2019-04-12 13:15:18 +00:00
|
|
|
called (separated by a colon).
|
|
|
|
|
|
|
|
For a typical Django project, invoking Uvicorn would look like:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2019-04-12 13:15:18 +00:00
|
|
|
.. code-block:: shell
|
|
|
|
|
2022-10-26 09:51:15 +00:00
|
|
|
python -m uvicorn myproject.asgi:application
|
2019-04-12 13:15:18 +00:00
|
|
|
|
|
|
|
This will start one process listening on ``127.0.0.1:8000``. It requires that
|
|
|
|
your project be on the Python path; to ensure that run this command from the
|
|
|
|
same directory as your ``manage.py`` file.
|
|
|
|
|
2022-10-26 09:51:15 +00:00
|
|
|
In development mode, you can add ``--reload`` to cause the server to reload any
|
|
|
|
time a file is changed on disk.
|
|
|
|
|
2019-04-12 13:15:18 +00:00
|
|
|
For more advanced usage, please read the `Uvicorn documentation <Uvicorn_>`_.
|
|
|
|
|
2022-10-26 09:51:15 +00:00
|
|
|
Deploying Django using Uvicorn and Gunicorn
|
|
|
|
===========================================
|
|
|
|
|
|
|
|
Gunicorn_ is a robust web server that implements process monitoring and automatic
|
|
|
|
restarts. This can be useful when running Uvicorn in a production environment.
|
|
|
|
|
|
|
|
To install Uvicorn and Gunicorn, use the following:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2022-10-26 09:51:15 +00:00
|
|
|
.. code-block:: shell
|
|
|
|
|
|
|
|
python -m pip install uvicorn gunicorn
|
|
|
|
|
|
|
|
Then start Gunicorn using the Uvicorn worker class like this:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2022-10-26 09:51:15 +00:00
|
|
|
.. code-block:: shell
|
|
|
|
|
|
|
|
python -m gunicorn myproject.asgi:application -k uvicorn.workers.UvicornWorker
|
|
|
|
|
2019-04-12 13:15:18 +00:00
|
|
|
.. _Uvicorn: https://www.uvicorn.org/
|
2022-10-26 09:51:15 +00:00
|
|
|
.. _Gunicorn: https://gunicorn.org/
|