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

后端 未结 6 2097
天命终不由人
天命终不由人 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:35

    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'
    

提交回复
热议问题