I’m trying to make a program that does this:
- Calls program 1 in a thread
- Calls program 2 in another thread
- If program 1 ends first, kill program 2, or vice-versa.
I’ve been trying for a long time and these are my current problem:
- I can’t import Thread module (not threading!).
- Function or Class, that is the question.
I already know how to call the processes using subprocess.Popen and how to kill them usign a command line function. I also know how to get the PID.
Here’s my actual code:
import threading, subprocess, shlex
class Prog1(threading.Thread):
def __init__(self, arg=''):
self.arg = arg
threading.Thread.__init__(self)
def run(self):
p = subprocess.Popen(shelx.split(self.arg))
global p.pid
subprocess.Popen(shelx.split("kill -9 " + q.pid))
class Prog2(threading.Thread):
def __init__(self, arg=''):
self.arg = arg
threading.Thread.__init__(self)
def run(self):
q = subprocess.Popen(shelx.split(self.arg))
global q.pid
subprocess.Popen(shelx.split("kill -9 " + p.pid))
To kill all processes if any of them has exited you could call
process.wait()for each process in a separate thread and usethreading.Eventto signal if any of the processes ended:Output