I have the below piece of code.
import asyncio
async def gen_random_numbers():
for i in range(1, 3):
await asyncio.sleep(2)
yield [i for i in range(1, 11)]
async def random_processor():
async for i, numbers in enumerate(gen_random_numbers()):
print(f"working with the batch {i} and processing {numbers}")
asyncio.run(random_processor())
But this throws an error
async for i, numbers in enumerate(gen_random_numbers()):
TypeError: 'async_generator' object is not iterable
One way to fix this is remove the enumerate and keep another variable to keep track of it and use it.
Is there a way to handle this using enumerate alone?
Using asyncstdlib ,
import asyncstdlib
async for i, numbers in asyncstdlib.enumerate(gen_random_numbers()):
print(f"working with the batch {i} and processing {numbers}")
should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.