0

I do have a code that runs the same python script with different arguments at the same time in different Shell windows.

The following code do this, but I need do monitor if any subprocess created have finished its execution, and then stop all other subprocesses running, how can I do this monitoring on Linux?

I have tried to check the PID status, but it changes to 'zombie' after the terminal is opened


import multiprocessing
import subprocess
import psutil
import shlex

if __name__ == '__main__':

    arg_list = ["gnome-terminal -- python3 teste.py ./get_data/datasets/training_out.xlsx 140.10.61.1 0 2 102 4 OUTPUT 0 632 MEMORY 0 3885 1 0",
            "gnome-terminal -- python3 teste.py ./get_data/datasets/training_out2.xlsx 140.10.61.1 0 2 102 1 OUTPUT 0 272 MEMORY 0 3885 1 1",
            "gnome-terminal -- python3 teste.py ./get_data/datasets/training_in.xlsx 140.10.61.1 0 2 102 2 INPUT 0 632 MEMORY 0 3885 1 2",
            "gnome-terminal -- python3 teste.py ./get_data/datasets/training_in2.xlsx 140.10.61.1 0 2 102 2 INPUT 0 272 MEMORY 0 3885 1 3",
            "gnome-terminal -- python3 teste.py ./get_data/datasets/training_in3.xlsx 140.10.61.1 0 2 102 1 INPUT 0 274 MEMORY 0 3885 1 4"             
            ]

    p1 = subprocess.Popen(shlex.split(arg_list[0]))
    psProcess1 = psutil.Process(pid=p1.pid)

    p2 = subprocess.Popen(shlex.split(arg_list[1]))
    psProcess2 = psutil.Process(pid=p2.pid)

    p3 = subprocess.Popen(shlex.split(arg_list[2]))
    psProcess3 = psutil.Process(pid=p3.pid)

    p4 = subprocess.Popen(shlex.split(arg_list[3]))
    psProcess4 = psutil.Process(pid=p4.pid)

    p5 = subprocess.Popen(shlex.split(arg_list[4]))
    psProcess5 = psutil.Process(pid=p5.pid)

Veysel Olgun
  • 552
  • 1
  • 3
  • 15
  • 1
    A zombie is a process that is no longer running, but where its parent has not checked in to see if it exited yet. Any invocation of the `wait()` syscall -- to see if it's running and, if it exited, how that happened -- will remove the zombie from the process table. – Charles Duffy Aug 08 '22 at 00:10
  • 2
    See `Popen.wait()`, `Popen.poll()`, etc (`poll()` uses the wait syscall under the hood; so does everything else that sets the `returncode` attribute). You don't need `psutil` for this, and in fact _shouldn't_ use it; unlike the wait syscall used by the parent, psutil doesn't reap zombies. Think of it this way: `wait()` is what you use to check on _your own_ children; `psutil` is what you use to check on _other processes'_ children. – Charles Duffy Aug 08 '22 at 00:12

0 Answers0