liveBook · Manning

Async for

As explained in Python Concurrency with asyncio

async for is Python syntax that asynchronously retrieves successive values from an asynchronous iterable until iteration is complete.

async for iterates over an asynchronous generator or other asynchronous iterable, awaiting each value as it becomes available. Unlike a regular for loop, it handles values produced through coroutines and is designed for asynchronous work such as delayed computation or streaming database results. section 5.7 section 5.7.1

Keep these rules in mind:

  • Use async for instead of a normal for loop or next when iterating over an asynchronous generator. section 5.7.1
  • Expect values to arrive sequentially; async for awaits each generated coroutine rather than running generated coroutines concurrently. section 5.7.1
  • Use it with a database cursor to process streamed rows one at a time and keep only a limited number of records in memory. section 5.7.2

Iterating over delayed values

Define an asynchronous generator with `async def`, pause before each `yield`, and consume it with `async for number in async_generator`. The loop prints each number after its delay completes.

Listing 5.15 Streaming results one by one
import asyncpg
import asyncio
import asyncpg
 
 
async def main():
    connection = await asyncpg.connect(host='127.0.0.1',
                                       port=5432,
                                       user='postgres',
                                       database='products',
                                       password='password')
 
    query = 'SELECT product_id, product_name FROM product'
    async with connection.transaction():
        async for product in connection.cursor(query):
            print(product)
 
    await connection.close()
 
 
asyncio.run(main())

Shows async for consuming an asyncpg cursor inside a transaction while streaming product rows individually. section 5.7.2

How Python Concurrency with asyncio covers it

How deep it goes
Explains syntax, generator differences, sequential awaiting, and cursor usage, while stopping short of concurrent iteration.
Examples
Demonstrates delayed positive integers and product records streamed through an asyncpg cursor.
What it assumes
Assumes regular generators and for loops, then builds asynchronous generators and their specialized iteration syntax from that contrast.
What you can do after
Apply it via Python asynchronous generators and asyncpg cursors to consume delayed values or database rows incrementally.
How it connects
Connects async for with asynchronous generators, coroutines, asyncio, database cursors, transactions, and streaming query results.

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.