我想知道如何在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中启动进程,然后在按下按钮后结束它们。
答案 0 :(得分:1)
不要在子流程中启动pro_b()
,只需直接调用pro_b()
即可。我认为pro_b()
子进程最终没有引用pro_a()
子进程。如果您直接致电pro_b()
,那么您将从父流程中删除pro_a()
子流程。