liveBook · Manning

Async view

As explained in Python Concurrency with asyncio

An async view is a web request handler implemented as an asynchronous callable that can suspend while awaiting operations.

An async view is a Django request handler implemented as an asynchronous callable. It can suspend at await points while asynchronous operations run, making it useful for requests to external APIs. section 9.4

Django’s ASGI support enables async views, while its WSGI support remains available. Async support is still evolving, and the ORM is synchronous, so blocking work requires care. section 9.4

Aggregating concurrent HTTP requests

An async view can use aiohttp to create a client session, build several request coroutines, await them with asyncio.gather, collect exceptions, and render response status, timing, and body length in a table.

Handle synchronous operations explicitly:

  • Wrap blocking functions with asgiref.sync.sync_to_async and await the resulting callable. section 9.4.1
  • Keep thread_sensitive=True when shared state depends on a specific thread; otherwise, False can use separate threads and avoid blocking Django’s main thread. section 9.4.1

How Python Concurrency with asyncio covers it

How deep it goes
Explains implementation with aiohttp and asyncio.gather, then addresses blocking functions through sync_to_async.
Examples
Demonstrates concurrent URL requests, tabular response summaries, and configurable blocking sleeps.
What it assumes
Assumes Django fundamentals and introduces async request handling alongside synchronous-library limitations.
What you can do after
Build it from scratch using Django, aiohttp, asyncio.gather, and sync_to_async.
How it connects
Connects Django with ASGI, WSGI, asyncio, aiohttp, and sync_to_async.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.