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.
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 .
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.
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.
As well as WSGI, Django also supports deploying on ASGI, the emerging Python standard for asynchronous web servers and applications.
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:
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).
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.
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.
No, lifespan
protocol doesn't work with Django 3.0.
See this ticket: https://code.djangoproject.com/ticket/31508
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
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
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With