有没有办法检查子进程是否仍在运行? [英] Is there a way to check if a subprocess is still running?

查看:95
本文介绍了有没有办法检查子进程是否仍在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 中使用 subprocess.Popen 启动了许多子进程.我想检查一个这样的过程是否已经完成.我找到了两种检查子流程状态的方法,但两者似乎都强制流程完成.一种是使用 process.communicate() 并打印返回码,如这里.另一个是简单地调用 process.wait() 并检查它是否返回 0.

I'm launching a number of subprocesses with subprocess.Popen in Python. I'd like to check whether one such process has completed. I've found two ways of checking the status of a subprocess, but both seem to force the process to complete. One is using process.communicate() and printing the returncode, as explained here. Another is simply calling process.wait() and checking that it returns 0.

有没有一种方法可以检查进程是否仍在运行而无需等待它完成?

Is there a way to check if a process is still running without waiting for it to complete if it is?

推荐答案

输出:...一种检查进程是否仍在运行的方法...

Ouestion: ... a way to check if a process is still running ...

你可以这样做:

p = subprocess.Popen(...
"""
A None value indicates that the process hasn't terminated yet.
"""
poll = p.poll()
if poll is None:
  # p.subprocess is alive

Python » 3.6.1 文档 popen-objects

使用 Python:3.4.2 测试

这篇关于有没有办法检查子进程是否仍在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆