【发布时间】:2020-01-15 15:03:46
【问题描述】:
使用asyncio.create_subprocess_exec 时,将返回asyncio.subprocess.process。 documentation 表示不存在 poll 或 is_alive 类型的方法。似乎wait 或communicate 提供了查看进程是否正在运行的唯一方法,但它们正在阻塞调用,并且通信的异步版本没有超时选项。
有没有一种很好的方法来检查 asyncio 子进程是否以 非阻塞 方式处于活动状态?
对于is_alive 样式函数,我能想到的最好的方法是:
import asyncio
async def is_alive(proc):
try:
await asyncio.wait_for(proc.wait(), 0.001)
except asyncio.TimeoutError:
return True
else:
return False
虚拟用例:
async def foo():
proc = await asyncio.create_subprocess_exec('sleep', '5')
i = 0
res = True
while res:
res = await is_alive(proc)
print(f"[{i}] is_alive: {res}")
# ... do foo stuff while we wait ...
await asyncio.sleep(1)
i += 1
loop = asyncio.get_event_loop()
loop.run_until_complete(foo())
输出:
[0] is_alive: True
[1] is_alive: True
[2] is_alive: True
[3] is_alive: True
[4] is_alive: True
[5] is_alive: False
【问题讨论】:
标签: python python-3.x subprocess python-asyncio