snippetpythonModeratepending
Python async generators and async iteration
Viewed 0 times
async generatorasync forasync iterationstreamingpagination
Problem
Need to produce values asynchronously, like streaming API responses or paginated database results.
Solution
Async generators yield values from async operations:
import asyncio
import aiohttp
# Async generator for paginated API
async def fetch_all_pages(url):
async with aiohttp.ClientSession() as session:
next_url = url
while next_url:
async with session.get(next_url) as resp:
data = await resp.json()
for item in data['results']:
yield item
next_url = data.get('next')
# Async generator for streaming lines
async def stream_lines(reader):
async for line in reader:
stripped = line.decode().strip()
if stripped:
yield stripped
# Consuming async generators
async def main():
# async for loop
async for user in fetch_all_pages('/api/users'):
print(user['name'])
# Collect into list
users = [u async for u in fetch_all_pages('/api/users')]
# First N items
count = 0
async for item in fetch_all_pages('/api/items'):
process(item)
count += 1
if count >= 100:
break
asyncio.run(main())Why
Async generators combine lazy evaluation with async I/O, enabling memory-efficient processing of large datasets from async sources.
Context
Python async code processing streams or paginated data
Revisions (0)
No revisions yet.