3

In python 3.3 I can do the following

def _gen(): for i in range(3): yield i def gen(): yield from _gen() for i in gen(): print(i) >>> 0 >>> 1 >>> 2 

Can I do the same within a python 3.6 asyncio coroutine? (Warning, contrived example)

async def _gen(): for i in range(3): yield await get_num(i) # get_num is a coroutine async def gen(): yield from _gen() # Syntax error! for i in gen(): print(i) 

I need to define gen as

async def gen(): async for i in _gen(): yield i 

But it seems there should be a way to delegate to the other coroutine as we could with yield from

3
  • It's not yet implemented. Commented Apr 23, 2018 at 21:32
  • @Vincent what indicates that it will be implemented at some point? Commented Apr 24, 2018 at 14:51
  • Because it would make sense. The limitation here is that it would require some serious effort as PEP 525 points out, and the gain is quite limited. Commented Apr 24, 2018 at 15:00

1 Answer 1

3

yield from is not supported in Python 3.6 due to the reasons described in PEP 525:

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 
Sign up to request clarification or add additional context in comments.

3 Comments

I find it hard to understand why yield from would require any kind of change to how asynchronous generators are implemented. Couldn't yield from inside async def simply desugar to an async for equivalent of the regular yield from desugaring?
@user4815162342 I believe asynchronous generators (and plain ones) are implemented in C, not Python. Code by link you provided is probably showcase, not real part of CPython source code.
Of course, the code only shows the desugaring that could be performed by the compiler, and the actual implementation is in the bytecode evaluator. But the PEP claims that the implementation of async generators would need to change, without giving a reason. After all, yield from could just be syntactic sugar for the kind of loop shown in g2.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.