पाठ 24 / 26

ASGI and WSGI Servers

Never use the development server in production.

gunicorn, uvicorn, workers

Django's runserver and uvicorn with --reload are for development. In production, run Django under a WSGI server (gunicorn) or an ASGI server (uvicorn, Daphne, Granian) when using async views or websockets, with several worker processes behind a reverse proxy (nginx, a cloud load balancer) that terminates TLS. FastAPI runs on ASGI servers such as uvicorn, often with multiple workers. Serve static files via WhiteNoise or a CDN, and run migrations as a deployment step.

From laptop to production

Serve apps with production servers, scale sensibly, and review with a checklist.

Three ideas: servers, performance, checklist.
Figure 8.1 — Servers, performance and checklist.

Production commands (sketch)

Not run here; check the framework documentation for your version.

# Django (WSGI)
python manage.py collectstatic --noinput
python manage.py migrate --noinput
gunicorn shopsite.wsgi:application --workers 4 --bind 0.0.0.0:8000

# Django (ASGI) or FastAPI
uvicorn shopsite.asgi:application --workers 4 --host 0.0.0.0 --port 8000
uvicorn main:app --workers 4 --host 0.0.0.0 --port 8000 --proxy-headers

Size workers from measurements

Start with about two workers per CPU core and adjust using load tests and memory limits.

त्वरित जाँच: Which server type runs FastAPI apps?

  • The Django admin
  • A WSGI-only server
  • An ASGI server such as uvicorn
  • A static file server
Answer

An ASGI server such as uvicorn — FastAPI is ASGI.