WSGI (Web Server Gateway Interface) là interface đồng bộ giữa Python web app và server (Gunicorn, uWSGI) — mỗi request một worker thread/process, request xong worker rảnh.
- Đơn giản, ổn định, là mặc định của Django. ASGI (Asynchronous Server Gateway Interface) là interface bất đồng bộ, hỗ trợ
async defview, WebSocket, HTTP/2, long-polling. - Server: Uvicorn, Daphne, Hypercorn.
# WSGI
gunicorn config.wsgi:application --workers 4 --bind 0.0.0.0:8000
# ASGI
uvicorn config.asgi:application --workers 4 --host 0.0.0.0 --port 8000Chọn WSGI cho app CRUD thuần (chiếm khoảng 95% case Django) — đơn giản, library nào cũng support. Chọn ASGI khi cần WebSocket (Django Channels), SSE, hoặc nhiều async I/O song song (gọi nhiều external API cùng lúc).
Một hiểu lầm quan trọng: đổi sang ASGI không tự động tăng tốc view sync. View sync vẫn chạy trong threadpool, hiệu năng không đổi. Lợi chỉ thấy rõ khi view thực sự viết bằng async def + await (async HTTP client, ORM .aget()). Mặc định cứ WSGI cho đến khi có nhu cầu cụ thể.
WSGI (Web Server Gateway Interface) is the synchronous interface between a Python web app and the server (Gunicorn, uWSGI) — one request per worker thread/process, the worker is free when the request ends. Simple, stable, Django's default.
ASGI (Asynchronous Server Gateway Interface) is the async interface — supports async def views, WebSocket, HTTP/2, long-polling. Servers: Uvicorn, Daphne, Hypercorn.
# WSGI
gunicorn config.wsgi:application --workers 4 --bind 0.0.0.0:8000
# ASGI
uvicorn config.asgi:application --workers 4 --host 0.0.0.0 --port 8000Which to pick:
- WSGI for plain CRUD apps (~95% of Django) — simple, libraries ready.
- ASGI when you need WebSocket (Django Channels), SSE, or many async I/O calls (external APIs in parallel).
Pitfall: Switching to ASGI does not automatically speed up sync views. They still run in a threadpool. The win shows up only when views actually use async def + await (async HTTP client, ORM .aget()). Default to WSGI until you have a concrete need.