Step 7. Async Django¶
This step covers two independent projects: 7A (hello_app, a sync vs async comparison) and 7B (notes_chat_app, a WebSocket group chat). The first is a standalone application on SQLite under Uvicorn. The second is a production-like stack with Docker, PostgreSQL, Redis, Daphne and Django Channels.
Prerequisites
Before you start, make sure you have completed:
- Step 1. Hello Django: the first
HttpResponse, URL routing - Step 2. Bootstrap Notes: ModelForm, PRG, Bootstrap CRUD
- Step 3. CRUD and architecture: Services & Selectors, PostgreSQL
- Step 4. Templates and Forms: Crispy Forms, Dashboard, Context Processor
- Step 5. Auth and Security: Sessions, Password Security, IDOR, Group Sharing
- Step 6. Testing: TestCase, fixtures, Selenium
7A vs 7B: two projects in one step¶
| 7A: hello_app | 7B: notes_chat_app | |
|---|---|---|
| Project | hello_app (a separate learning project) | notes_chat_app (production-like) |
| How to run | uvicorn notes_project.asgi:application --port 8001 |
docker compose up --build |
| Database | SQLite | PostgreSQL (Docker) |
| ASGI server | Uvicorn | Uvicorn (in Docker) |
| Channel layer | InMemoryChannelLayer | RedisChannelLayer |
| WebSocket | Demo in section 18 of README_8 | GroupChatConsumer (full implementation) |
| Daphne | Optional | First in INSTALLED_APPS |
| Redis | Not needed | REDIS_URL → RedisChannelLayer |
| Goal | Compare sync and async views architecturally | Show where async is the only possible architecture |
What you will learn¶
Part 7A: Async Views¶
- Why an
async def viewdoes not become more useful just because you wroteasync - What a lazy QuerySet is and where SQL actually runs
- How
async forreplacesforwhen iterating over a QuerySet - When and why
sync_to_asyncis needed fortransaction.atomic() - How to run Django under ASGI with Uvicorn
- How to write tests for async views with
AsyncClient - That
aupdate()+F()is an atomic SQL UPDATE without loading the object - Where async is justified and where it is unnecessary complexity
Part 7B: WebSocket chat¶
- Why HTTP is not suitable for real time and where async becomes a necessity
- What the asyncio event loop and cooperative multitasking are
- What WebSocket is and how it differs from HTTP (handshake, persistent connection)
- What ASGI is and how it differs from WSGI
- How
ProtocolTypeRoutercombines HTTP and WebSocket in a single ASGI process - What a Consumer is and how it differs from a View
- How
AuthMiddlewareStackauthenticates WebSocket connections - What
database_sync_to_asyncis and why the Consumer needs it - How the channel layer delivers messages to all participants (pub/sub)
- Why
load_historyreturns alistand not a QuerySet - XSS protection in the WebSocket client:
escapeHtml() - Testing the Consumer with
WebsocketCommunicatorandTransactionTestCase
Reading order¶
Part 7A: Sync vs Async Django (hello_app)¶
- 7A. Sync vs Async overview: project architecture, file table, sync and async request flows, when async is justified
- 7A. Async Views and the ORM:
async_selectors.py,async_services.py,async_views.py, lazy vs evaluation,sync_to_async - 7A. Benchmark and testing:
AsyncClient, sync vs async tests, Postman, benchmark, Docker Compose
Part 7B: WebSocket chat (notes_chat_app)¶
- 7B. The WebSocket protocol: HTTP vs WebSocket, handshake, asyncio event loop, why sync does not fit a chat
- 7B. The ASGI stack: WSGI vs ASGI,
ProtocolTypeRouter,asgi.py,routing.py,AuthMiddlewareStack - 7B. Channel Layers and Daphne: Daphne in
INSTALLED_APPS,InMemoryChannelLayervsRedisChannelLayer,socket_timeout=None - 7B. Consumer: lifecycle, the full
GroupChatConsumercode,database_sync_to_async, broadcast - 7B. WebSocket JS client: the Vanilla JS WebSocket API, the full client, XSS protection, the full flow
- Checkpoint: 7A and 7B checklists, Consumer tests, common mistakes, navigation