Python asyncio.subprocess():如何查看是否还是运行
Python asyncio.subprocess(): How to see whether it is still running
p
个 class asyncio.subprocess.Process
个对象由
创建
p = await asyncio.create_subprocess_exec(...)
如何以非阻塞的方式判断系统调用是否还在运行?
因为 asyncio Process
没有 poll()
方法,你可以结合 Process.wait()
和 asyncio.wait_for()
,使用一个小的超时允许 wait()
检查进程是否完成,如果完成则设置 returncode
属性。
async def is_running(proc):
with contextlib.suppress(asyncio.TimeoutError):
await asyncio.wait_for(proc.wait(), 1e-6)
return proc.returncode is None
p
个 class asyncio.subprocess.Process
个对象由
p = await asyncio.create_subprocess_exec(...)
如何以非阻塞的方式判断系统调用是否还在运行?
因为 asyncio Process
没有 poll()
方法,你可以结合 Process.wait()
和 asyncio.wait_for()
,使用一个小的超时允许 wait()
检查进程是否完成,如果完成则设置 returncode
属性。
async def is_running(proc):
with contextlib.suppress(asyncio.TimeoutError):
await asyncio.wait_for(proc.wait(), 1e-6)
return proc.returncode is None