如何终止子进程中打开的所有子进程

How can I terminate all child processes opened in subprocess

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 脚本都包含一个无限循环,因此,我正在尝试创建一个函数来终止任意数量的进程由上述函数生成。 我试过

x.terminate()

但这行不通,我认为应该终止所有子进程,但它并没有这样做,我认为它可能会终止最后启动的进程或类似的东西,但我的问题是, 我怎样才能终止我的第一个函数启动的任意数量的进程?

将所有子流程放在一个列表中而不是覆盖 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()