简体   繁体   English

如何检查是否只有一个 Popen 子进程仍在运行?

[英]How to check if not one but any Popen subprocess at all is still running?

I know there is the possibility to check if a subprocess is still running with .poll().我知道可以使用 .poll() 检查子进程是否仍在运行。 So for example:例如:

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

In my case, I'm running multiple of the same subprocesses at the same time and I store them inside of a list called proc.就我而言,我同时运行多个相同的子进程,并将它们存储在名为 proc 的列表中。 Each time when I need a new subprocess I just call:每次当我需要一个新的子进程时,我就调用:

proc.append(subprocess.Popen([sys.executable,...

.poll() won't take list values so does anyone has a working example for me on how I can check if any subprocess at all is still running with the subprocesses stored inside of a list? .poll() 不会采用列表值,所以有没有人为我提供有关如何检查是否有任何子进程仍在运行,并且存储在列表中的子进程的工作示例?

This is my update so far:到目前为止,这是我的更新:

proc.append(subprocess.Popen([sys.executable,....


def evaluate():
    global proc
    global p
    p = []
    for t in proc:
        print(t.poll())
        if t.poll() is None:
            p.append(0)
        else:
            p.append(1)


evaluate()
if 1 in p:
    #some tasks running
else:
    #no task running

As suggest by Matiiss you can loop over the list or access items in the list by index eg根据 Matiiss 的建议,您可以遍历列表或按索引访问列表中的项目,例如

>>> slp = []
>>> slp.append(subprocess.Popen(['sleep', '40']))
>>> slp.append(subprocess.Popen(['sleep', '40']))
>>> slp[1].poll()
>>> slp[1].poll()
>>> slp[1].poll()
>>> slp[1].poll()
>>> slp[1].poll()
>>> slp[1].poll()
0

or:或者:

>>> for i in slp:
...  i.poll()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM