# ASGI and WSGI Servers — Django / FastAPI

Source: https://www.skillbyai.com/en/django-fastapi/d-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.](assets/figures/django-fastapi/section-8-map.svg) — Figure 8.1 — Servers, performance and checklist.

## Production commands (sketch)

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

```bash
# 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.

**Quiz:** Which server type runs FastAPI apps?

- [ ] The Django admin
- [ ] A WSGI-only server
- [x] An ASGI server such as uvicorn
- [ ] A static file server

*Answer:* An ASGI server such as uvicorn. FastAPI is ASGI.
