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

pip install gunicorn
pip install gunicorn

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

Workers

A common rule of thumb:

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

Example:

gunicorn "wsgi:app" --workers 3 --bind 0.0.0.0:8000
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.

If this helped you, consider buying me a coffee ☕

Buy me a coffee

Was this page helpful?

Let us know how we did