提问者:小点点

Python-延迟后运行异步功能,不阻塞其他代码


这是我的代码:

async def outer():
    # if this while loop was not broken in 5 seconds, do something
    while True:
        # some code with breaks
    

通常我需要一个非阻塞异步定时器。


共1个答案

匿名用户

def outer():
    async def loop():
         #while loop here
  

    task = asyncio.create_task(loop())
    done, pending = await asyncio.wait([task], return_when=asyncio.FIRST_COMPLETED, timeout=5)
    if task in done:
        # loop has completed
    else:
       # loop is incomplete

  • 一个同步io。等待

相关问题