0
0
Flaskframework~10 mins

WSGI servers (Gunicorn, uWSGI) in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - WSGI servers (Gunicorn, uWSGI)
Start Flask App
WSGI Server Receives HTTP Request
WSGI Server Calls Flask App
Flask App Processes Request
Flask App Returns Response
WSGI Server Sends HTTP Response
Client Receives Response
The WSGI server acts as a middleman between the web and the Flask app, receiving requests, passing them to Flask, then sending back responses.
Execution Sample
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 'Hello from Flask!'

# Run with: gunicorn app:app
A simple Flask app that returns a greeting, served by Gunicorn as the WSGI server.
Execution Table
StepActionWSGI Server StateFlask App StateResponse Sent
1WSGI server starts and listensListening for requestsNot called yetNo response
2HTTP request arrivesRequest receivedNot called yetNo response
3WSGI server calls Flask app with requestProcessing requestRunning home() routeNo response yet
4Flask app returns 'Hello from Flask!'Waiting to send responseResponse readyNo response yet
5WSGI server sends HTTP response to clientIdle, waiting for next requestIdleResponse sent: 'Hello from Flask!'
6Client receives responseIdleIdleResponse received by client
💡 Request cycle ends after response is sent and client receives it
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
WSGI Server StateIdleProcessing requestWaiting to send responseIdleIdle
Flask App StateNot calledRunning home()Response readyIdleIdle
Response SentNoNoNoYesYes
Key Moments - 3 Insights
Why does the WSGI server call the Flask app instead of handling the request itself?
The WSGI server acts like a waiter taking orders; it passes the request to Flask (the kitchen) to prepare the response. See execution_table step 3 where the server calls the Flask app.
When does the Flask app actually run the code to create the response?
The Flask app runs when the WSGI server calls it with the request, shown in step 3 and 4 of the execution_table where the home() function runs and returns the response.
What happens if the WSGI server does not send the response back to the client?
The client waits and sees no result. The cycle ends only after the server sends the response, as shown in step 5 and 6 where the response is sent and received.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the WSGI server state at step 4?
AListening for requests
BIdle
CWaiting to send response
DRequest received
💡 Hint
Check the 'WSGI Server State' column at step 4 in the execution_table.
At which step does the Flask app finish processing and have the response ready?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Flask App State' column to see when the response is ready.
If the WSGI server never sends the response, what would the 'Response Sent' variable show after step 5?
ANo
BMaybe
CYes
DError
💡 Hint
Refer to the 'Response Sent' variable in variable_tracker after step 5.
Concept Snapshot
WSGI servers like Gunicorn or uWSGI receive HTTP requests from clients.
They call the Flask app with the request.
Flask processes and returns a response.
The server sends this response back to the client.
This cycle repeats for each request.
WSGI servers act as the middleman between web and Flask.
Full Transcript
WSGI servers such as Gunicorn and uWSGI serve as intermediaries between the web and Flask applications. When a client sends an HTTP request, the WSGI server receives it and calls the Flask app with this request. The Flask app processes the request by running the appropriate route function and returns a response string. The WSGI server then sends this response back to the client. This process repeats for every incoming request, allowing Flask to focus on application logic while the WSGI server handles communication with the web. The execution table shows each step from server start, request receipt, Flask processing, to response delivery. Variables track the server and app states and when the response is sent. Understanding this flow helps beginners see how Flask apps run in real web environments using WSGI servers.