Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#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 Mariusz Felisiak, 2 years ago

Cc: Thomas Grainger Carlton Gibson added

comment:2 by Simon Willison, 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 Simon Willison, 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 Simon Willison, 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 Simon Willison, 2 years ago

The FastAPI deployment documentation talks about reasons to use Gunicorn here: https://fastapi.tiangolo.com/deployment/server-workers/

comment:6 by Simon Willison, 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 Simon Willison, 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 Simon Willison, 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 Simon Willison, 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 Mariusz Felisiak, 2 years ago

Has patch: set
Owner: changed from nobody to Simon Willison
Status: newassigned
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

comment:11 by GitHub <noreply@…>, 2 years ago

Resolution: fixed
Status: assignedclosed

In 022a864a:

Fixed #34109 -- Updated instructions for running with Uvicorn.

Co-authored-by: Paolo Melchiorre <paolo@…>

comment:12 by Carlton Gibson <carlton.gibson@…>, 2 years ago

In ccb830e2:

[4.1.x] Fixed #34109 -- Updated instructions for running with Uvicorn.

Co-authored-by: Paolo Melchiorre <paolo@…>
Backport of 022a864a5d1de28b853f009566a24be251e90d96 from main

Note: See TracTickets for help on using tickets.
Back to Top