0
0
Djangoframework~10 mins

Gunicorn as WSGI server in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Gunicorn as WSGI server
Start Gunicorn Command
Gunicorn Loads WSGI App
Gunicorn Creates Worker Processes
Workers Listen for HTTP Requests
Request Received
Worker Calls Django WSGI Application
Django Processes Request
Response Returned to Worker
Worker Sends HTTP Response to Client
End
Gunicorn starts, loads the Django WSGI app, creates workers that listen for requests, then workers handle requests by calling Django and sending back responses.
Execution Sample
Django
gunicorn myproject.wsgi:application
# Gunicorn starts and runs Django app
# Listens for HTTP requests
# Handles requests with workers
This command starts Gunicorn as the WSGI server to run the Django application and handle incoming HTTP requests.
Execution Table
StepActionGunicorn StateWorker StateDjango App StateOutput
1Run 'gunicorn myproject.wsgi:application'Starting master processNo workers yetNot loadedGunicorn master process starts
2Gunicorn loads WSGI applicationMaster process loaded appNo workers yetWSGI app loadedReady to spawn workers
3Gunicorn spawns worker processesMaster runningWorkers spawned and listeningWSGI app readyWorkers ready for requests
4HTTP request arrivesMaster runningWorker receives requestWaiting for callRequest received by worker
5Worker calls Django WSGI appMaster runningWorker processing requestProcessing requestDjango app processes request
6Django returns responseMaster runningWorker has responseResponse readyResponse generated
7Worker sends HTTP responseMaster runningWorker sends response to clientIdleClient receives HTTP response
8Wait for next requestMaster runningWorkers listeningIdleReady for next request
9Shutdown command receivedMaster shutting downWorkers terminatingApp closingGunicorn stops
💡 Gunicorn stops when shutdown command is received or process is killed
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 7Final
Gunicorn MasterNot runningRunning, app loadedRunning, workers spawnedRunningRunningStopped
Worker ProcessesNoneNoneSpawned, listeningProcessing requestSent responseTerminated
Django WSGI AppNot loadedLoadedLoadedProcessing requestIdleClosed
HTTP RequestNoneNoneNoneReceivedRespondedNone
Key Moments - 3 Insights
Why does Gunicorn create multiple worker processes?
Gunicorn creates multiple workers (see Step 3) so it can handle many requests at the same time without waiting for one to finish before starting another.
What happens when a worker receives an HTTP request?
At Step 4, the worker listens and receives the request, then calls the Django WSGI app to process it (Step 5), ensuring the request is handled properly.
Why does the Django app appear 'idle' after sending a response?
After sending the response (Step 7), the Django app waits for the next request, so it is idle until called again by a worker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of the worker processes after Step 3?
ASpawned and listening for requests
BNot created yet
CProcessing a request
DTerminated
💡 Hint
Check the 'Worker State' column at Step 3 in the execution table.
At which step does the Django WSGI app process the HTTP request?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look at the 'Django App State' column to see when it is 'Processing request'.
If Gunicorn did not spawn multiple workers, what would change in the execution table?
ADjango app would never load
BWorkers would be 'None' after Step 3
CMaster process would not start
DHTTP requests would be processed faster
💡 Hint
Refer to the 'Worker Processes' variable in variable_tracker after Step 3.
Concept Snapshot
Gunicorn is a WSGI server that runs Django apps.
It starts a master process and spawns multiple worker processes.
Workers listen for HTTP requests and call the Django WSGI app.
Django processes requests and returns responses via workers.
Multiple workers allow handling many requests at once.
Gunicorn stops when shutdown is triggered.
Full Transcript
Gunicorn acts as a WSGI server for Django by starting a master process that loads the Django WSGI application. It then creates multiple worker processes that listen for incoming HTTP requests. When a request arrives, a worker receives it and calls the Django WSGI app to process the request. Django generates a response which the worker sends back to the client. After sending the response, the worker waits for the next request. This setup allows handling many requests concurrently. Gunicorn stops when a shutdown command is received.