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

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

    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

提交回复
热议问题