2.1.1 WSGI
WSGI stands for Web Server Gateway Interface.
It is the older, standard interface that allows a Python web server to communicate with a Python web application.
Simple architecture
Browser
↓
HTTP Request
↓
WSGI Server
↓
WSGI
↓
Python Web App
↓
Response
For example, with a traditional Flask application:
Browser
↓
Gunicorn
↓
WSGI
↓
Flask
WSGI vs ASGI
| WSGI | ASGI | |
|---|---|---|
| Full name | Web Server Gateway Interface | Asynchronous Server Gateway Interface |
| Main style | Synchronous | Async + synchronous |
async/await | Not designed for it | Designed for it |
| WebSockets | ❌ | ✅ |
| Common framework | Flask, Django | FastAPI, Starlette |
| Common server | Gunicorn | Uvicorn |
The key difference
With WSGI:
def get_data():
data = database_query() # waits here
return data
The request handling is traditionally synchronous.
With ASGI:
async def get_data():
data = await database_query()
return data
While the database operation is waiting, the event loop can work on other requests.
Think of the evolution like this
WSGI
↓
Synchronous Python web apps
ASGI
↓
Async-capable Python web apps
↓
async/await + WebSockets + concurrent I/O
For your FastAPI learning, the chain worth remembering is:
Uvicorn
↓
ASGI
↓
Starlette
↓
FastAPI
↓
Your application
Whereas a common Flask stack is:
Gunicorn
↓
WSGI
↓
Flask
↓
Your application
Gunicorn
Gunicorn is a Python application server. Its full name is Green Unicorn.
The important thing is that Gunicorn is not the same thing as WSGI.
Gunicorn = server
WSGI = interface/standard
Flask = web framework
How they work together
For a traditional Flask application:
Browser
↓
HTTP request
↓
Gunicorn
↓
WSGI
↓
Flask
↓
Your Python code
For example:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return {"message": "Hello"}
You could run it with:
gunicorn app:app
Here:
app:app
│ │
│ └── Flask application object
└────── Python file (app.py)
Why not just use Python?
During development, Flask can run its own development server:
flask run
But for production, you normally put a proper application server such as Gunicorn in front of your application.
Gunicorn can create multiple worker processes:
Gunicorn
/ | \
Worker 1 Worker 2 Worker 3
↓ ↓ ↓
Flask Flask Flask
This allows multiple requests to be handled concurrently across workers.
Gunicorn vs Uvicorn
This is particularly important for your FastAPI learning:
Flask
↓
WSGI
↓
Gunicorn
while:
FastAPI
↓
ASGI
↓
Uvicorn
Gunicorn is traditionally a WSGI server, while Uvicorn is an ASGI server.
There are also production setups where Gunicorn manages Uvicorn workers for FastAPI:
Gunicorn
/ | \
↓ ↓ ↓
Uvicorn Uvicorn Uvicorn
↓ ↓ ↓
FastAPI applications
So remember:
lightbulbGunicorn manages Python worker processes; WSGI/ASGI defines how the server communicates with the web application.