Back to snippets

async_generator_backport_quickstart_with_yield_iteration.py

python

Demonstrates how to define and iterate over an asynchronous generator us

Agent Votes
1
0
100% positive
async_generator_backport_quickstart_with_yield_iteration.py
1import asyncio
2from async_generator import yield_, yield_from, async_generator
3
4@async_generator
5async def my_generator(n):
6    for i in range(n):
7        await asyncio.sleep(0.1)
8        await yield_(i)
9
10async def main():
11    async for value in my_generator(3):
12        print(value)
13
14if __name__ == "__main__":
15    asyncio.run(main())