Python 结束GUI在退出时启动的所有进程(守护进程=True不工作)

Python 结束GUI在退出时启动的所有进程(守护进程=True不工作),python,user-interface,parallel-processing,multiprocessing,Python,User Interface,Parallel Processing,Multiprocessing,当用户结束主程序UI时,如何确保所有进程都关闭?我已尝试将进程设置为daemon=True,但这不起作用。我看到其他线程表示,您应该使用一个信号,并不断检查是否发出了“退出”信号,但这对我来说没有意义,因为如果GUI现在不存在,GUI代码将不再执行 在这种情况下,流程如何知道GUI是否消失了:(其中执行sys.exit(app.exec_389;())) 找到了答案。您可以在QWidget对象中重新定义closeEvent函数。像这样: def closeEvent(self, event):

当用户结束主程序UI时,如何确保所有进程都关闭?我已尝试将进程设置为
daemon=True
,但这不起作用。我看到其他线程表示,您应该使用一个信号,并不断检查是否发出了“退出”信号,但这对我来说没有意义,因为如果GUI现在不存在,GUI代码将不再执行

在这种情况下,流程如何知道GUI是否消失了:(其中执行
sys.exit(app.exec_389;())


找到了答案。您可以在
QWidget
对象中重新定义
closeEvent
函数。像这样:

def closeEvent(self, event):
    for p in self.processlist:
        p.terminate()
以下是更新的代码段:

from PyQt4 import QtCore, QtGui
import multiprocessing as mp
import sys
import time

def f(data):
    for i in range(0,10**5):
        data[0] += 1
        time.sleep(.1)

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self)
        self.centralwidget = QtGui.QWidget(self)
        self.top_level_layout = QtGui.QGridLayout(self.centralwidget)
        self.setCentralWidget(self.centralwidget)
        self.centralwidget.setLayout(self.top_level_layout)
        self.process_button = QtGui.QPushButton("Start Process")
        self.top_level_layout.addWidget(self.process_button, 0, 0)
        QtCore.QObject.connect(self.process_button, QtCore.SIGNAL("clicked()"), self.start_process)

        self.processlist = list()
        self.manager = mp.Manager()
        self.data = self.manager.list([1])
        for i in range(0, 10):
            self.processlist.append(mp.Process(target=f, args=(self.data,)))
            self.processlist[-1].daemon = True

    def closeEvent(self, event):
        for p in self.processlist:
            p.terminate()

    def start_process(self):
        for p in self.processlist:
            p.start()

if __name__ == "__main__":
    app = QtGui.QApplication([])
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())
from PyQt4 import QtCore, QtGui
import multiprocessing as mp
import sys
import time

def f(data):
    for i in range(0,10**5):
        data[0] += 1
        time.sleep(.1)

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self)
        self.centralwidget = QtGui.QWidget(self)
        self.top_level_layout = QtGui.QGridLayout(self.centralwidget)
        self.setCentralWidget(self.centralwidget)
        self.centralwidget.setLayout(self.top_level_layout)
        self.process_button = QtGui.QPushButton("Start Process")
        self.top_level_layout.addWidget(self.process_button, 0, 0)
        QtCore.QObject.connect(self.process_button, QtCore.SIGNAL("clicked()"), self.start_process)

        self.processlist = list()
        self.manager = mp.Manager()
        self.data = self.manager.list([1])
        for i in range(0, 10):
            self.processlist.append(mp.Process(target=f, args=(self.data,)))
            self.processlist[-1].daemon = True

    def closeEvent(self, event):
        for p in self.processlist:
            p.terminate()

    def start_process(self):
        for p in self.processlist:
            p.start()

if __name__ == "__main__":
    app = QtGui.QApplication([])
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())