结束退出时GUI启动的所有进程(daemon = True not working)

时间:2013-12-02 22:43:24

标签: python user-interface parallel-processing multiprocessing

如果用户结束主程序UI,您如何确保关闭所有进程?我已尝试将流程设置为daemon = True,但这不起作用。我已经看到其他线程声明你应该使用一个信号并不断检查是否已经发出“退出”信号,但是这对于放置这个脚本的位置没有意义,因为GUI代码不再是如果GUI现在消失则执行。

在这种情况下,如果GUI消失,进程将如何知道:(执行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 start_process(self):
        for p in self.processlist:
            p.start()

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

1 个答案:

答案 0 :(得分:0)

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

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_())