Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Asynchronous Generators in Python

Give Feedback

In this lesson you’ll learn how to create an asynchronous generator:

async def square_odds(start, stop):
    for odd in odds(start, stop):
        await asyncio.sleep(2)
        yield odd ** 2

You’ll also see how to loop over values asynchronously using an async for loop.

Comments & Discussion

karncx on Aug. 30, 2019

Why does the async for take number of steps * 2 seconds? Why does it not save time like asyncio.gather?

tevakrief on Sept. 12, 2019

Hello ! Same question as karncx, why does it take 2s each time ?

Thanks !

Become a Member to join the conversation.