Python - 运行 延迟后的异步函数,不阻塞其他代码
Python - run an async funtion after delay and do not block other code
这是我的代码:
async def outer():
# if this while loop was not broken in 5 seconds, do something
while True:
# some code with breaks
通常我需要一个非阻塞的异步定时器。
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
参考文献:
这是我的代码:
async def outer():
# if this while loop was not broken in 5 seconds, do something
while True:
# some code with breaks
通常我需要一个非阻塞的异步定时器。
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