question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception thrown when running Django with ASGI/Uvicorn/Gunicorn

See original GitHub issue

Hi,

I’m honestly not sure if this is a uvicorn issue, a Gunicorn issue or a Django issue - but I’ll start here, since I don’t see the same exceptions if I just replace uvicorn with Daphne.

We’re running some experiments using ASGI instead of WSGI for our Django projects, and our go-to has been uvicorn, since we already use this in some starlette projects - we love using it.

And it works - for the most parts. But when Gunicorn starts for the first time, we get an exception (per worker): Django can only handle ASGI/HTTP connections, not lifespan.

I don’t have the specific trace, but it happens in the Django ASGI handler.

Now, I realise that this happens inside Django code - but what makes me post here first, is that it doesn’t happen if we use Daphne. I’m assuming (without knowing anything about the internals, so I might be very wrong), that when uvicorn is spun up within Gunicorn, it attempts to set up a lifespan protocol, and this causes the issue with Django. However, as far as I can see, it’s not possible to set the lifespan implementation to off, when the uvicorn worker class is used.

If I’m completely off base here, please let me know, and I’ll try to direct this to a different place.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

20reactions
HenrikOssipoffcommented, Jul 3, 2020

For the time begin we’ve made this custom worker class to get rid of the issue as per your suggestion, and it does indeed work:

from uvicorn.workers import UvicornWorker as BaseUvicornWorker


class UvicornWorker(BaseUvicornWorker):
    CONFIG_KWARGS = {"loop": "uvloop", "http": "httptools", "lifespan": "off"}
0reactions
euri10commented, Sep 29, 2020

I will close this @HenrikOssipoff in favor of https://github.com/encode/uvicorn/issues/266 as it’s the same gunicorn worker config problematic that is easily solved by subclassing the UvicornWorker which you did perfectly !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running django with ASGI? - gunicorn - Stack Overflow
import os from django.core.asgi import get_asgi_application ... I have opened this question about it: Uvicorn async workers are still working synchronously.
Read more >
How To Set Up an ASGI Django App with ... - DigitalOcean
Testing Gunicorn's Ability to Serve the Project. In this tutorial, you will be using Gunicorn paired with Uvicorn to deploy the application.
Read more >
Channels Documentation - Read the Docs
Channels is a project that takes Django and extends its abilities beyond HTTP - to handle WebSockets, chat protocols,.
Read more >
Django on Azure - beyond "hello world" - Anthony Shaw
By default, Azure Web Apps will terminate HTTPS traffic and connect to your Django application using the WSGI entry-point and Gunicorn.
Read more >
Creating your first project | Django 3 By Example - Third Edition
Our first Django project will be building a complete blog. ... or uWSGI, or as an ASGI application using a server like Uvicorn...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found