Python - 终止在 main 中启动的进程

Python - Terminate a Process started in main

我想知道如何在 main 中启动进程然后在 def 函数中终止它。在我的代码中,我有一个运行感兴趣的应用程序的进程,然后是另一个运行使用 Tkinter 等待按下按钮的简单 GUI 的进程。按下此按钮时,我希望进程被终止。例如:

def pro_a():
    #Runs the application

def pro_b():
    root.mainloop() # Runs the GUI

def buttonCallBack()
    #I want to terminate the processes here
    #I've tried doing: p1.terminate()

b = Button(frame, .........., command = buttonCallBack)
b.place(......)

if __name__ == '__main__':
    p1 = Process(target=pro_b)
    p2 = Process(target=pro_a)
    p1.start()
    p2.start()

当我尝试这样做时,出现错误:AttributeError: 'NoneType' object has no attribute 'terminate'

但是当我尝试在 main 中终止它时,它起作用了。但这不是我想要的。明确地说,我需要在 main 中启动进程,然后在按下按钮后结束它们。

不要在子进程中启动 pro_b(),直接调用 pro_b()。我认为 pro_b() 子进程在没有引用 pro_a() 子进程的情况下结束。如果您直接调用 pro_b(),那么您将从父进程中杀死 pro_a() 子进程。