[Python-ideas] async/await in Python

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


Yury Selivanov wrote:

> The only problem is that if we apply this flag to coroutines
> in asyncio, a lot of code can be broken.  I.e. whenever someone
> passed around generator-objects, or used syntax like
> 'o = gen(); yield from o' there will be a *runtime* error.

Hmmm, using the decorator approach, there might need to
be a way of flagging a function so that both __call__
and __cocall__ work. That should be enough, since in
PEP 3152 there is nothing special about the iterator
returned by __cocall__.

> def __aenter__(self): // a function returning awaitable
>    if something():
>        return coroutine_func()
>    else:
>        return future()

In a world where PEP 3152 were established, things like
tasks and futures would implement __cocall__, and if it
were to have something like __aenter__ it would be
defined as a cofunction. So you would write that as

   codef __aenter__(self):
      if something():
         return cocall coroutine_func()
      else:
         return cocall future()

-- 
Greg


More information about the Python-ideas mailing list