Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django + Uvicorn

I'm trying to use Django 3.0 with Uvicorn and getting this on start:

INFO:     Started server process [96219]
INFO:     Waiting for application startup.
INFO:     ASGI 'lifespan' protocol appears unsupported.
INFO:     Application startup complete.

I could turn lifespan off with the --lifespan off flag, but is there a way to have it work with Django? A quick search for Django + lifespan seems to not return anything.

like image 291
aroooo Avatar asked Apr 19 '20 06:04

aroooo


People also ask

How do I use Django with Uvicorn?

Running Django in Uvicorn Uvicorn needs to be called with the location of a module containing an ASGI application object, followed by what the application is called (separated by a colon). This will start one process listening on 127.0. 0.1:8000 .

What is Uvicorn?

Uvicorn is an ASGI web server implementation for Python. Until recently Python has lacked a minimal low-level server/application interface for async frameworks. The ASGI specification fills this gap, and means we're now able to start building a common set of tooling usable across all async frameworks.

Does FastAPI need Uvicorn?

The main thing you need to run a FastAPI application in a remote server machine is an ASGI server program like Uvicorn. There are 3 main alternatives: Uvicorn: a high performance ASGI server.

Does Django support ASGI?

As well as WSGI, Django also supports deploying on ASGI, the emerging Python standard for asynchronous web servers and applications.

How do I run a uvicorn application in Django?

When Uvicorn is installed, a uvicorn command is available which runs ASGI applications. Uvicorn needs to be called with the location of a module containing an ASGI application object, followed by what the application is called (separated by a colon). For a typical Django project, invoking Uvicorn would look like:

What is uvicorn in Linux?

Uvicorn is an ASGI server based on uvloop and httptools, with an emphasis on speed. When Uvicorn is installed, a uvicorn command is available which runs ASGI applications. Uvicorn needs to be called with the location of a module containing an ASGI application object, followed by what the application is called (separated by a colon).

Is it possible to run Django programmatically from Gunicorn?

If you deploy Django async under ASGI, chances are you need an ASGI server like Uvicorn. In production, you can use Uvicorn with Gunicorn, but in development you might want to use Uvicorn standalone, which can also run programmatically from a Python script.

Is it possible to use uvicorn with Gunicorn?

In production, you can use Uvicorn with Gunicorn, but in development you might want to use Uvicorn standalone, which can also run programmatically from a Python script. This gives also the ability to debug your asynchronous Django project locally with any IDE.


2 Answers

No, lifespan protocol doesn't work with Django 3.0.

See this ticket: https://code.djangoproject.com/ticket/31508

like image 97
Kuba Misiorny Avatar answered Oct 12 '22 13:10

Kuba Misiorny


Here's the initial setup I used with django 3.1 / 3.2

EDIT: some rough performance benchmarks: https://github.com/allen-munsch/benchmark-django-fastapi

old version

your_django/old_wsgi.py:

import os

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_django.settings")

application = Cling(get_wsgi_application())

Previously run as:

newrelic-admin run-program gunicorn your_django.old_wsgi \
-k eventlet --log-file - --timeout 60

new version

your_django/asgi.py:

import os

from django.core.asgi import get_asgi_application
from django.contrib.staticfiles.handlers import ASGIStaticFilesHandler

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pm.settings")

application = ASGIStaticFilesHandler(
    get_asgi_application()
)

And to run

gunicorn your_django.asgi --log-level=debug -k uvicorn.workers.UvicornWorker \
--log-file - --timeout 60

# OR for local dev

uvicorn --reload your_django.asgi

related:

  • https://stackoverflow.com/a/67275668/2026508
like image 1
jmunsch Avatar answered Oct 12 '22 11:10

jmunsch