Theory of Intelligence
BACKEND ENGINEERING•Apis And Python Servers•Python Servers•2.1 WSGI & ASGI

2.1.1 WSGI

Subtopics: WSGI and ASGI.

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

TEXT
Browser
   ↓
HTTP Request
   ↓
WSGI Server
   ↓
WSGI
   ↓
Python Web App
   ↓
Response

For example, with a traditional Flask application:

TEXT
Browser
   ↓
Gunicorn
   ↓
WSGI
   ↓
Flask

WSGI vs ASGI

WSGIASGI
Full nameWeb Server Gateway InterfaceAsynchronous Server Gateway Interface
Main styleSynchronousAsync + synchronous
async/awaitNot designed for itDesigned for it
WebSockets❌✅
Common frameworkFlask, DjangoFastAPI, Starlette
Common serverGunicornUvicorn

The key difference

With WSGI:

PYTHON
def get_data():
    data = database_query()  # waits here
    return data

The request handling is traditionally synchronous.

With ASGI:

PYTHON
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

TEXT
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:

TEXT
Uvicorn
   ↓
  ASGI
   ↓
Starlette
   ↓
FastAPI
   ↓
Your application

Whereas a common Flask stack is:

TEXT
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.

TEXT
Gunicorn = server
WSGI     = interface/standard
Flask    = web framework

How they work together

For a traditional Flask application:

TEXT
Browser
   ↓
HTTP request
   ↓
Gunicorn
   ↓
WSGI
   ↓
Flask
   ↓
Your Python code

For example:

PYTHON
from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return {"message": "Hello"}

You could run it with:

BASH
gunicorn app:app

Here:

TEXT
app:app
│   │
│   └── Flask application object
└────── Python file (app.py)

Why not just use Python?

During development, Flask can run its own development server:

BASH
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:

TEXT
                 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:

TEXT
Flask
  ↓
WSGI
  ↓
Gunicorn

while:

TEXT
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:

TEXT
              Gunicorn
             /   |   \
            ↓    ↓    ↓
        Uvicorn Uvicorn Uvicorn
            ↓     ↓      ↓
          FastAPI applications

So remember:

lightbulb

Gunicorn manages Python worker processes; WSGI/ASGI defines how the server communicates with the web application.

Discuss theoretical concepts, mathematical formulas, and architectural foundations.

Supports inline code with `backticks`
No comments yet. Be the first to start the discussion!