Sub topic 9
Running a Python App with Gunicorn
This chapter assumes that you know basic Linux commands, file permissions, HTTP requests, and Python virtual environments. You also need a small Python web application that exposes a WSGI callable. WSGI is a standard interface between a Python web application and an application server.
What Gunicorn does
Gunicorn is a Python application server. It receives HTTP requests, calls your Python application, and sends the application’s responses back to the client.
A development server is useful while writing code, but it usually handles only limited traffic and may not manage failures well. Gunicorn starts multiple worker processes so that several requests can be handled at once. A worker is a process that handles application requests.
The basic request flow is:
Create a minimal WSGI application
Create a file named app.py:
def application(environ, start_response):
body = b"Hello from Gunicorn\n"
start_response("200 OK", [
("Content-Type", "text/plain"),
("Content-Length", str(len(body))),
])
return [body]The application function is the WSGI callable. Gunicorn calls it for each request. The environ argument contains request information, such as the method and path. The start_response function receives the HTTP status and response headers. The function returns an iterable containing the response body as bytes.
Create and activate a virtual environment, then install Gunicorn:
python3 -m venv venv
. venv/bin/activate
python -m pip install gunicornStart Gunicorn with three workers:
gunicorn --workers 3 --bind 127.0.0.1:8000 app:applicationThe value app:application means “import the application object from app.py.” The --bind option chooses the address and port. Binding to 127.0.0.1 makes Gunicorn reachable only from the same server. This is a good default when Nginx will be the public-facing web server.
Test it from another terminal:
curl -i http://127.0.0.1:8000/You should receive a 200 OK response and the text Hello from Gunicorn.
Choose workers and diagnose startup errors
Each synchronous worker handles one request at a time. More workers can improve concurrency, but every worker uses memory and loads your application separately. Start with a small number and measure resource use rather than choosing a large number automatically.
Common startup errors include:
ModuleNotFoundError: Gunicorn is running from the wrong directory, or the virtual environment lacks a dependency.Failed to find attribute: The callable name after the colon is incorrect.Address already in use: Another process already owns port8000.- A worker exits immediately: Check the terminal output for an import error or application exception.
For a production deployment, run Gunicorn under a service manager such as systemd. The service manager starts it at boot, records its output, and restarts it after a failure. Nginx can then proxy public requests to Gunicorn instead of exposing the application server directly.
Exercise
What does app:application mean in a Gunicorn command?
Show answer
It tells Gunicorn to import the object named application from the Python module app.