PEP 0492 adds new __await__
magic method. Object that implements this method becomes future-like object and can be awaited using await
. It
I didn't understand why I can't yield from native coroutine inside __await__
, but looks like it's possible to yield from generator coroutine inside __await__
and yield from native coroutine inside that generator coroutine. It works:
async def new_sleep():
await asyncio.sleep(2)
class Waiting:
def __await__(self):
@asyncio.coroutine
def wrapper(coro):
return (yield from coro)
return (yield from wrapper(new_sleep()))