Topic 02 / 40

ASGI vs WSGI Server Lifecycles

~12 min read  //  Django Series  //  Coding India

1. Deep Architecture

WSGI operates under a synchronous, blocking model (one thread per request). ASGI (Asynchronous Server Gateway Interface) splits the request-response lifecycle into distinct asynchronous events, letting single-threaded event loops handle thousands of concurrent WebSocket connections using Python’s asyncio runtime.

2. The Feynman Gatekeeper

[KNOWLEDGE CHECK] Why does a database call inside a standard WSGI thread block all other operations on that thread, and how does ASGI’s event loop bypass this limitation?

3. The Code

# config/asgi.py
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')

application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    # WebSocket routing is added here in Phase 3
})

4. The Funnel

Stat Level-Up: Asynchronous Initiator (Lvl 1).
Sanjaya Integration: Preparing Sanjaya to accept long-running video uploads and real-time status updates.