Python终止在main中启动的进程

2024-06-01 02:52:42 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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中启动进程,然后在按下按钮后结束它们。在


Tags: thetarget进程maindefrunsguiprocess