[Python-ideas] async/await in Python

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Apr 21 08:06:31 CEST 2015


Yury Selivanov wrote:
> Since it's prohibited to use 'yield' in coroutines, we may
> allow it in later Python versions.  (Although I'm certain, that
> we'll need a new keyword for 'yield' in coroutines.)

The obvious way to spell it would be "async yield" (and
we would probably also want "async yield from").

> 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.

I don't think it would be necessary to duplicate genobject.c.
The implementation would probably go something like this:

* Use of 'async yield' in an async def function sets a new
flag marking it as an async generator.

* 'async yield x' raises StopIteration(x) so that it becomes
the return value of the current __anext__ call.

* Returning from an async generator raises StopAsyncIteration.

I'm not sure about 'async yield from'. If I think about this
any more my head will probably start to hurt, so I'll
stop now.

I agree that this might be better left for a later PEP.

-- 
Greg




More information about the Python-ideas mailing list