[Python-ideas] async/await in Python
Yury Selivanov
yselivanov.ml at gmail.com
Tue Apr 21 18:20:24 CEST 2015
Hi Greg,
On 2015-04-21 2:06 AM, Greg Ewing wrote:
> 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").
Agree! Although it looks a little bit strange, I think
we will need to clearly indicate that this is not an ordinary
yield expression.
>
>> 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.
The semantics you describe looks correct. Although I highly
doubt that it's possible to fit all this magic into existing
generators implementation (it should be possible, but make
it more complicated, harder to support and maybe a bit slower).
>
> 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.
>
I think that's what we should do. I'd be happy to help
with that later PEP.
BTW, there is one more thing about requiring a keyword
to make a coroutine call. If we decide to implement
coroutine-generators one day, a simple call to
coroutine-generator should return a coroutine-generator
object:
async def foo():
async yield 1
foo() # <- coroutine-generator object
In the other hand, if you don't have an 'async yield'
expression, then
foo()
would raise an error. I'm not sure I like this duality.
Thanks,
Yury
More information about the Python-ideas
mailing list