'TypeError: 'async_generator' object is not iterable

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 ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source