How can I await inside future-like object's __await__? How can I await inside future-like object's __await__? python-3.x python-3.x

How can I await inside future-like object's __await__?


Use direct __await__() call:

async def new_sleep():    await asyncio.sleep(2)class Waiting:    def __await__(self):        return new_sleep().__await__()

The solution was recommended by Yury Selivanov (the author of PEP 492) for aioodbc library


Short version: await foo can be replaced by yield from foo.__await__()


Combining all the ideas from the other answers -

in the simplest case, just delegating to another awaitable works:

def __await__(self):    return new_sleep().__await__()

This works because the __await__ method returns an iterator (see PEP 492), so returning another __await__'s iterator is fine.

This means, of course, that we can't change the suspension behavior of the original awaitable at all. The more general approach is to mirror the await keyword and use yield from - this lets us combine multiple awaitables' iterators into one:

def __await__(self):    # theoretically possible, but not useful for my example:    #yield from something_else_first().__await__()    yield from new_sleep().__await__()

Here's the catch: this is not doing exactly the same thing as the first variant! yield from is an expression, so to do exactly the same as before, we need to also return that value:

def __await__(self):    return (yield from new_sleep().__await__())

This directly mirrors how we would write proper delegation using the await syntax:

    return await new_sleep()

extra bit - what's the difference between these two?

def __await__(self):    do_something_synchronously()    return new_sleep().__await__()def __await__(self):    do_something_synchronously()    return (yield from new_sleep().__await__())

The first variant is a plain function: when you call it, do_... is executed and an iterator returned. The second is a generator function; calling it doesn't execute any of our code at all! Only when the returned iterator is yielded for the first time will do_... be executed. This makes a difference in the following, a little contrived situation:

def foo():    tmp = Waiting.__await__()    do_something()    yield from tmp


To await inside a __await__ function, use the following code:

async def new_sleep():    await asyncio.sleep(1)class Waiting:    def __await__(self):        yield from new_sleep().__await__()        print('first sleep')        yield from new_sleep().__await__()        print('second sleep')        return 'done'