1

I know there is the possibility to check if a subprocess is still running with .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. 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?

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
Gordian
  • 101
  • 8
  • 2
    why can't you loop over the list? `for process in proc: print(process.poll())` – Matiiss Oct 26 '21 at 15:29
  • How would I then define if a subprocess is still running? Because the variable that defines it would be overridden each time inside of the loop. – Gordian Oct 26 '21 at 17:42
  • you save them to a list so the reference is saved there, did you even try doing that? – Matiiss Oct 26 '21 at 17:44
  • Yes, I tried the last two hours to figure it out myself. However, I have not yet succeeded. Where can I post more code because it seems not to work inside of this comment? – Gordian Oct 26 '21 at 17:53
  • I've updated my question with where I'm currently at. Thanks for your help so far, really appreciate that. – Gordian Oct 26 '21 at 18:03
  • btw it should be `print(t.poll())` – Matiiss Oct 26 '21 at 18:08
  • I think my current script doesn't work because t.poll() responding 'None' would be the same as if it would actually respond nothing because it's empty. I fixed the print command. – Gordian Oct 26 '21 at 18:20
  • 2
    Why not use the builtin `any`, with a generator expression e.g. `if any(t.poll() is None for t in proc): print('something is running')`? – Oli Oct 26 '21 at 20:01

1 Answers1

1

As suggest by Matiiss you can loop over the list or access items in the list by index e.g.

>>> 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()

With reference to your comment:

>>> slp = []
>>> slp.append([subprocess.Popen(['sleep', '40']), None])
>>> slp.append([subprocess.Popen(['sleep', '40']), None])
>>> if slp[1][0].poll() == 0:
...    slp[1][1] = "Finished"
... 
>>> slp
[[<subprocess.Popen object at 0x7f17b1280a90>, None], [<subprocess.Popen object at 0x7f17b0bb0550>, None]]
>>> if slp[1][0].poll() == 0:
...    slp[1][1] = "Finished"
... 
>>> slp
[[<subprocess.Popen object at 0x7f17b1280a90>, None], [<subprocess.Popen object at 0x7f17b0bb0550>, 'Finished']]

The loop differs because the loop pulls out the list item, which is itself a list [subprocess, variable]:

>>> slp = []
>>> slp.append([subprocess.Popen(['sleep', '40']), None])
>>> slp.append([subprocess.Popen(['sleep', '40']), None])
>>> for i in slp:
...     if i[0].poll() == 0:
...         i[1] = "finished"
... 
>>> slp
[[<subprocess.Popen object at 0x7f17b0ba7ef0>, None], [<subprocess.Popen object at 0x7f17b0b64cf8>, None]]

>>> for i in slp:
...     if i[0].poll() == 0:
...         i[1] = "finished"
... 
>>> slp
[[<subprocess.Popen object at 0x7f17b1280a90>, 'finished'], [<subprocess.Popen object at 0x7f17b0bb0550>, 'finished']]
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • How would I then define if a subprocess is still running? Because the variable that defines it would be overridden each time inside of the loop. – Gordian Oct 26 '21 at 17:41
  • That is a different question but simplistically you would check within the loop and assign that to another variable. Your list `proc` could be a list of lists, where the second item is a `still running` variable for example. Or you could define a secondary result list corresponding to the `proc` list. See my edit. – Rolf of Saxony Oct 26 '21 at 19:25