Why can’t I ‘yield from’ inside an async function?

According to PEP 525, which introduces asyncronous generators in Python 3.6:

Asynchronous yield from

While it is theoretically possible to implement yield from support for
asynchronous generators, it would require a serious redesign of the
generators implementation.

yield from is also less critical for asynchronous generators, since
there is no need provide a mechanism of implementing another
coroutines protocol on top of coroutines. And to compose asynchronous
generators a simple async for loop can be used:

async def g1():
    yield 1
    yield 2

async def g2():
    async for v in g1():
        yield v

As you can see, the answer boils down to “it would be too hard to implement, and you don’t need it anyway”.

Leave a Comment