Skip to content

Django: Gunicorn vs Daphne, when to switch ASGI

Decide between Gunicorn-WSGI and Daphne-ASGI for a Django deployment based on WebSocket needs, async views, long-poll routes, and operational tradeoffs.

The default Django deployment shape — Gunicorn fronting wsgi.py — works fine for 90% of Django apps. The 10% where it doesn’t is also the 10% where teams reach for Daphne or Uvicorn without quite knowing why. This article makes the choice deterministic: WebSockets, server-sent events, long-poll, or substantial async def view code all push toward ASGI. Everything else stays on WSGI.

How to verify

Check what’s actually serving requests and which path traffic takes:

systemctl list-units --type=service | grep -E "gunicorn|daphne|uvicorn"
ss -ltnp | grep -E "(8000|/run/)"
python manage.py shell -c "from django.conf import settings; print(settings.WSGI_APPLICATION, settings.ASGI_APPLICATION)"
curl -s -o /dev/null -w "%{http_version}\n" http://127.0.0.1:8000/
grep -rE "async def|sync_to_async|database_sync_to_async" project/ app/ | head

If ASGI_APPLICATION is set and you’re running Gunicorn-WSGI, your async views are being downgraded — see below.

What’s happening

Django supports both server protocols. wsgi.py exposes a synchronous WSGI app — Gunicorn forks processes, each handles one request at a time per thread, and Django’s request-response cycle is sequential. asgi.py exposes an ASGI app — Daphne or Uvicorn runs an event loop per worker, each can hold thousands of open connections, and async def views run cooperatively without blocking the loop.

The critical detail is that WSGI and ASGI Django are the same Django. ORM calls are synchronous in both worlds — under ASGI, an async def view that calls Model.objects.get() blocks the loop unless wrapped in sync_to_async. So switching from Gunicorn to Daphne does not, by itself, make a slow app faster. It makes WebSockets possible, makes long-poll cheap, and lets httpx-based outbound calls run concurrently — but the ORM is still the bottleneck for most apps, and Gunicorn handles that bottleneck with a more mature operational model (max_requests recycling, --preload for shared memory, graceful restart on SIGHUP).

The other dimension is protocol shape. WSGI cannot speak WebSockets. Period. If you need WebSockets you need ASGI, full stop — Channels is built on this. SSE (server-sent events) works on WSGI but holds a thread for the lifetime of the connection, which exhausts Gunicorn’s worker pool at hundreds of clients; under ASGI it costs almost nothing.

Decision

  1. Stay on Gunicorn-WSGI if: all your views are def (sync), you have no WebSocket routes, you have no SSE routes, and your async needs are limited to a Celery task queue. This is the operational default. A typical config:

    # gunicorn.conf.py
    bind = "unix:/run/django-app.sock"
    workers = 2 * os.cpu_count() + 1
    worker_class = "sync"
    timeout = 60
    max_requests = 1000
    max_requests_jitter = 100
    preload_app = True
  2. Switch to Daphne/Uvicorn-ASGI if any of: you need real WebSockets (Channels), you serve SSE to more than a handful of concurrent clients, you have async def views that call external APIs over httpx and benefit from concurrency, or you’d otherwise need gevent workers (which have their own debugging hell). For ASGI under Gunicorn-managed Uvicorn workers — the easiest swap:

    # gunicorn.conf.py
    bind = "unix:/run/django-app.sock"
    workers = os.cpu_count()          # async workers, fewer needed
    worker_class = "uvicorn.workers.UvicornWorker"
    timeout = 60
    max_requests = 2000
    max_requests_jitter = 200
  3. Use Daphne directly (no Gunicorn) only if you’re using Channels. Daphne is the reference ASGI server for Channels — the Channels manual assumes it. For non-Channels ASGI Django, Uvicorn under Gunicorn is operationally simpler than running Daphne as a template unit per core.

  4. Wrap ORM calls in async views. Under ASGI, Model.objects.get() blocks the event loop. The mitigation is await Model.objects.aget() (Django 4.1+) or from asgiref.sync import sync_to_async for code paths not yet using async ORM:

    from asgiref.sync import sync_to_async
    
    async def my_view(request):
        user = await sync_to_async(User.objects.get)(pk=request.user.pk)
        return JsonResponse({"name": user.name})
  5. Run the two protocols separately if you want a hybrid. Split URL prefixes at Nginx: /api/ to Gunicorn-WSGI, /ws/ to Daphne-ASGI. You pay double the memory footprint of the codebase, but you get the operational maturity of Gunicorn for HTTP and the WebSocket capability of Daphne. Acceptable for large monoliths during a gradual migration.

  6. Don’t switch for “async is faster.” It isn’t, for Django ORM workloads. Switching ASGI without converting views to use the async ORM gets you a slower app with more moving parts. Benchmark first.

Common pitfalls

  • Setting ASGI_APPLICATION and running Gunicorn-WSGI: Django picks up the WSGI path silently. Any consumer routing in asgi.py is never reached. Symptom: WebSockets fail with 426 or 404.
  • Running gunicorn --worker-class uvicorn.workers.UvicornWorker with --preload: Uvicorn doesn’t tolerate preloading because forking after the event loop starts corrupts asyncio state. Drop --preload for ASGI workers.
  • Forgetting to bump proxy_read_timeout in Nginx when adding SSE or WebSocket routes — default 60 s closes long connections. Set to 3600 s for long-poll paths.
  • Running both gunicorn and daphne units against the same code without separating their venvs or environment files — a DJANGO_SETTINGS_MODULE collision crashes both.
  • Mixing async def and sync middleware. Async middleware in front of a sync view (or vice versa) makes Django insert an adapter that costs latency. Make middleware match the view it wraps.

For the full Channels deployment, see Deploying Django Channels for WebSocket production traffic. Stack Harbor’s managed operations practice runs Gunicorn-WSGI as the Django default and switches to Uvicorn-workers (or Daphne for Channels stacks) when a route in the codebase genuinely needs it — not because async is fashionable.