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

后端 未结 6 2071
天命终不由人
天命终不由人 2020-12-02 17:02

PEP 0492 adds new __await__ magic method. Object that implements this method becomes future-like object and can be awaited using await. It

6条回答
  •  温柔的废话
    2020-12-02 17:34

    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()))
    

提交回复
热议问题