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
