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
pip install gunicornpip install gunicornCreate a WSGI entrypoint
wsgi.pywsgi.py:
from myapp import create_app
app = create_app()from myapp import create_app
app = create_app()Run gunicorn
gunicorn "wsgi:app" --bind 0.0.0.0:8000gunicorn "wsgi:app" --bind 0.0.0.0:8000Workers
A common rule of thumb:
workers = 2 * CPU + 1workers = 2 * CPU + 1
Example:
gunicorn "wsgi:app" --workers 3 --bind 0.0.0.0:8000gunicorn "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.
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
