Gunicorn Web Server
Gunicorn is a popular WSGI server for Python.
It runs your Flask app with:
- multiple worker processes
- better stability than the dev server
Install
Section titled “Install”pip install gunicornCreate a WSGI entrypoint
Section titled “Create a WSGI entrypoint”wsgi.py:
from myapp import create_app
app = create_app()Run gunicorn
Section titled “Run gunicorn”gunicorn "wsgi:app" --bind 0.0.0.0:8000Workers
Section titled “Workers”A common rule of thumb:
workers = 2 * CPU + 1
Example:
gunicorn "wsgi:app" --workers 3 --bind 0.0.0.0:8000Timeouts
Section titled “Timeouts”If requests can be slow, configure timeouts:
--timeout
But also ask: should this work be moved to background jobs?
Logging
Section titled “Logging”Make sure logs go to stdout/stderr for containers:
- platforms can collect them automatically.
Visualize it
Section titled “Visualize it”Here’s a typical production topology where Nginx sits in front of Gunicorn, which runs your Flask app across multiple worker processes.
flowchart LR I["Internet"] --> N["Nginx (reverse proxy, static files)"] N --> G["Gunicorn (WSGI server, multiple workers)"] G --> F["Flask app"]
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading