[Python-ideas] async/await in Python
Yury Selivanov
yselivanov.ml at gmail.com
Tue Apr 21 18:13:40 CEST 2015
Yann,
On 2015-04-21 3:28 AM, Yann Kaiser wrote:
> On Tue, 21 Apr 2015 at 00:07 Yury Selivanov <yselivanov.ml at gmail.com> wrote:
[...]
>> If someone smarter than me can figure out a way to do this
>> in a non-confusing way that won't require duplicating genobject.c
>> and fourth of ceval.c I'd be glad to update the PEP.
>
> Well the above strategy could be imitated, multiplexing the yield channel
> between yields meant for the event loop and those meant for the
> asynchronous iteration protocol. It could be done with an additional
> parameter to YIELD_VALUE (I'm assuming await compiles down to it or
> YIELD_FROM ?), although it would be redundant for functions not using both
> concepts at the same time. Still, it's just one byte.
> YIELD_FROM... my head explosion warnings are also being set off.
>
> As Greg pointed out, the presence of yield in an async function would have
> it return an object also supporting __aiter__ and __anext__.
>
Yann, I think that there are no fundamental reasons
preventing us from having a coroutine-generator hybrid.
The semantics will probably be like that:
def foo(): # generator function
yield 1
foo() # generator object
async def bar(): # coroutine-generator function
async yield 1 # or just 'yield 1'
bar() # coroutine-generator object
Maybe it's possible to heavily hack genobject.c and
ceval.c (YIELD & YIELD_FROM implementations) to support this.
But I think that the amount of hacks will be so huge, that
we'll need a pair of new opcodes + new asyncgenobject.c, to
at least avoid performance penalties on regular generators.
Anyways, I think it's outside of the scope of PEP 492.
I think that the best thing we should do now, is to make
sure that we *can* have coroutine-generators in the future.
I think that PEP 492, in its current state leaves a room
for that.
If you want to continue the discussion on this topic, I
suggest you to clone the reference implementation and
implement your concept. But there is no guarantee that
it will land to PEP 492 though.
Thanks,
Yury
More information about the Python-ideas
mailing list