Exception thrown when running Django with ASGI/Uvicorn/Gunicorn
See original GitHub issueHi,
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:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
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:
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 !