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
bash
pip install gunicornbash
pip install gunicornCreate a WSGI entrypoint
wsgi.pywsgi.py:
python
from myapp import create_app
app = create_app()python
from myapp import create_app
app = create_app()Run gunicorn
bash
gunicorn "wsgi:app" --bind 0.0.0.0:8000bash
gunicorn "wsgi:app" --bind 0.0.0.0:8000Workers
A common rule of thumb:
workers = 2 * CPU + 1workers = 2 * CPU + 1
Example:
bash
gunicorn "wsgi:app" --workers 3 --bind 0.0.0.0:8000bash
gunicorn "wsgi:app" --workers 3 --bind 0.0.0.0:8000Timeouts
If requests can be slow, configure timeouts:
--timeout--timeout
But also ask: should this work be moved to background jobs?
Logging
Make sure logs go to stdout/stderr for containers:
- platforms can collect them automatically.
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"]
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
