0
0
Flaskframework~10 mins

Gunicorn for production serving in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Gunicorn for production serving
Write Flask app
Install Gunicorn
Run Gunicorn with app
Gunicorn starts workers
Workers listen for requests
Requests received -> handled by workers
Responses sent back to clients
Monitor and restart workers if needed
This flow shows how you write a Flask app, then use Gunicorn to run it in production by starting worker processes that handle incoming requests and send responses.
Execution Sample
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello, World!'

# Run with: gunicorn app:app
A simple Flask app that returns 'Hello, World!' at the home page, served by Gunicorn workers in production.
Execution Table
StepActionGunicorn ProcessWorker ProcessResult
1Start Gunicorn with 'gunicorn app:app'Master process startsNo workers yetGunicorn master ready
2Gunicorn spawns worker processesMaster manages workersWorkers start and load Flask appWorkers ready to accept requests
3Client sends HTTP requestMaster routes requestWorker receives requestWorker begins processing
4Worker calls Flask app route functionMaster waitsFlask app returns 'Hello, World!'Response generated
5Worker sends HTTP responseMaster waitsResponse sent to clientClient receives 'Hello, World!'
6Worker waits for next requestMaster monitors workersIdle, ready for next requestSystem stable
7If worker crashesMaster detects failureWorker restartsService continues without downtime
💡 Gunicorn master and workers run continuously to serve requests until stopped.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
Master ProcessNot runningRunning, managing workersRunningRunningRunningRunning
Worker ProcessesNoneStarted, loaded appReceived requestProcessed requestSent responseIdle, waiting
RequestNoneNoneReceived by workerBeing processedResponse sentNone
ResponseNoneNoneNoneGeneratedSent to clientNone
Key Moments - 3 Insights
Why does Gunicorn start multiple worker processes instead of just one?
Gunicorn starts multiple workers to handle many requests at the same time, improving performance and reliability. See execution_table step 2 where workers start and load the app.
What happens if a worker process crashes during request handling?
The master process detects the crash and restarts the worker automatically, so the service keeps running without downtime. This is shown in execution_table step 7.
Why don't we run the Flask app directly in production?
Flask's built-in server is for development only and can't handle many users or restart workers. Gunicorn manages multiple workers and is designed for production, as shown in the concept flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of worker processes after step 2?
AWorkers are idle and waiting for requests
BWorkers have crashed
CWorkers are started and have loaded the Flask app
DWorkers have sent responses to clients
💡 Hint
Check the 'Worker Process' column in execution_table at step 2
At which step does the worker process send the HTTP response back to the client?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look for 'Response sent to client' in the 'Result' column of execution_table
If the master process stops managing workers, what will happen according to the flow?
AWorkers will continue handling requests normally
BWorkers will crash and not restart
CWorkers will become master processes
DNothing changes, system runs as usual
💡 Hint
Refer to execution_table step 7 about master detecting worker failure and restarting
Concept Snapshot
Gunicorn runs Flask apps in production by starting a master process and multiple worker processes.
Workers handle incoming HTTP requests concurrently.
Master monitors workers and restarts them if they crash.
Run with: gunicorn app:app
This setup improves performance and reliability over Flask's built-in server.
Full Transcript
This visual execution shows how Gunicorn serves a Flask app in production. First, you write a Flask app with routes. Then you install Gunicorn and run it with your app. Gunicorn starts a master process that manages several worker processes. Each worker loads the Flask app and listens for HTTP requests. When a client sends a request, the master routes it to a worker. The worker calls the Flask route function, generates a response, and sends it back to the client. Workers stay ready for new requests. If a worker crashes, the master detects this and restarts it automatically. This setup allows your Flask app to handle many users reliably and efficiently in production.