#34109 closed Cleanup/optimization (fixed)
Uvicorn can be run without installing gunicorn
Reported by: | Simon Willison | Owned by: | Simon Willison |
---|---|---|---|
Component: | Documentation | Version: | 4.1 |
Severity: | Normal | Keywords: | |
Cc: | Thomas Grainger, Carlton Gibson | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/uvicorn/ currently says you should do this:
python -m pip install uvicorn gunicorn
Then:
gunicorn myproject.asgi:application -k uvicorn.workers.UvicornWorker
This isn't actually necessary: you can install just uvicorn
and then do this:
uvicorn myproject.asgi:application
Change History (12)
comment:1 by , 2 years ago
Cc: | added |
---|
comment:2 by , 2 years ago
Huh, that's surprising. I've been deploying Uvicorn apps for a few years now without using gunicorn at all.
https://github.com/django/django/pull/13455 seems to have made that decision based on the Uvicorn docs themselves at https://www.uvicorn.org/deployment/#gunicorn - which do recommend gunicorn for production, but don't explain why.
I'm going to try and work out WHY they recommend gunicorn, and maybe suggest a change to the Django docs that helps make that clear.
comment:3 by , 2 years ago
It looks like the recommendation to use gunicorn with uvicorn dates back to this commit four years ago: https://github.com/encode/uvicorn/commit/07c6c18945ab4f4167118e717e197adce3aee3da#diff-b4d68dc855d0f9476d3f2ee343853bd21bf82ea9960d0cf06661baa244439dd6R93-R111 - and prior to that fix gunicorn was baked into uvicorn itself.
comment:4 by , 2 years ago
Best answer I've found so far is this comment by Tom Christie: https://github.com/encode/uvicorn/issues/303#issuecomment-464154975
So: Gunicorn will give you multiple worker processes, as well as monitoring and restarting any crashed processes. We might start to have some of those things built directly into Uvicorn at some point, but for now if you want multiple processes you need to use Gunicorn or some other process manager such as circus.
comment:5 by , 2 years ago
The FastAPI deployment documentation talks about reasons to use Gunicorn here: https://fastapi.tiangolo.com/deployment/server-workers/
comment:6 by , 2 years ago
There's an interesting draft PR against Uvicorn here which updates their deployment documentation to reflect current recommendations.
https://github.com/encode/uvicorn/pull/1607/files
One of the new points in there is the idea that gunicorn isn't necessary if you are deploying inside a container - where using uvicorn alone works fine.
But... that PR is still being discussed. I don't think the advice there is stable and agreed upon yet.
comment:7 by , 2 years ago
The title of the page at https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/uvicorn/ is "How to use Django with Uvicorn".
As such, I think it's worth explaining both the with-gunicorn and without-gunicorn mechanisms. I'll update my PR to do that instead.
comment:8 by , 2 years ago
Updated my PR to add the Gunicorn content back in as a separate section: https://github.com/django/django/pull/16199/commits/ab2b704f652ecde718cf32d4b1e441a4087e92f1
comment:9 by , 2 years ago
Just talked to Andrew Godwin about this. He confirmed that Gunicorn isn't necessary when running applications in a container environment which features a separate layer that handles automatic restarts on failure, such as Kubernetes.
comment:10 by , 2 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
Status: | new → assigned |
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Cleanup/optimization |
This was added in c1442e1192057a3bf14aecbaa1b713eee139eaff, see PR.