Skip to content

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 gunicorn
bash
pip install gunicorn

Create 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:8000
bash
gunicorn "wsgi:app" --bind 0.0.0.0:8000

Workers

A common rule of thumb:

  • workers = 2 * CPU + 1workers = 2 * CPU + 1

Example:

bash
gunicorn "wsgi:app" --workers 3 --bind 0.0.0.0:8000
bash
gunicorn "wsgi:app" --workers 3 --bind 0.0.0.0:8000

Timeouts

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.

diagram Production deployment topology mermaid
Nginx reverse-proxies requests and serves static files, Gunicorn runs multiple workers, and each worker runs the Flask app

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did