def example_function(self):
number = self.lineEdit_4.text() #Takes input from GUI
start = "python3 /path/to/launched/script.py "+variable1+" "+variable2+" "+variable3 #Bash command to execute python script with args.
for i in range(0,number):
x = subprocess.Popen(start,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)#Launch another script as a subprocess
所以,这段代码多次启动另一个Python脚本,每个python脚本包含一个无限的while循环,所以,我试图创建一个函数来杀死上面函数生成的任意数量的进程。 我试过像
这样的东西x.terminate()
但是这只是不起作用,我认为应该杀死所有的子进程,但它不会这样做,我认为它可能会杀死最后一个启动的进程或类似的东西,但我的问题是,如何我可以杀死我的第一个函数启动的任何数量的进程吗?
答案 0 :(得分:0)
将所有子流程放在列表中,而不是覆盖x
变量
def example_function(self):
number = self.lineEdit_4.text() #Takes input from GUI
start = "python3 /path/to/launched/script.py "+variable1+" "+variable2+" "+variable3 #Bash command to execute python script with args.
procs = []
for i in range(0,number):
x = subprocess.Popen(start,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)#Launch another script as a subprocess
procs.append(x)
# Do stuff
...
# Now kill all the subprocesses
for p in procs:
p.kill()