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

wsgi.py:

python
from myapp import create_app
 
app = create_app()
bash
gunicorn "wsgi:app" --bind 0.0.0.0:8000

A common rule of thumb:

  • workers = 2 * CPU + 1

Example:

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

If requests can be slow, configure timeouts:

  • --timeout

But also ask: should this work be moved to background jobs?

Make sure logs go to stdout/stderr for containers:

  • platforms can collect them automatically.

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

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading