PyQt5 and multiprocessing - multiple processes cre

2019-07-25 11:11发布

Trying to implement multiprocessing within a PyQt5 GUI framework to process multiple files in a parallel process, preferably in a QThread() background. Running the code below seems to create the multiple processes but has additional issue that multiple GUI windows appear and everything seems locked out at that point.

import multiprocess as mp
from PyQt5.QtWidgets import QMainWindow

class MyApp(QMainWindow, myUiMainWindow):

    def __init__(self):
        super(self.__class__, self).__init()
        self.setupUI(self) 
        self.pushButton.clicked.connect(self.doMyStuff)

    def consumer(self, inQ, outQ):
        val = inQ.get()
        ret = self.process_single(val)
        outQ.put(ret)

    def process_single(self, f):
        <process each file f>
        <update progress bar>
        return f

    def doMyStuff(self):
        <get file_list from GUI Widget>
        n_w = len(file_list) if len(file_list) < 5 else 5
        inQ = mp.Queue()
        outQ = mp.Queue()

        workers = [mp.Process(target=consumer, args=(inQ, outQ) for i in range(n_w)]
        [w.start() for w in workers]
        [inQ.put(f) for f in file_list]
        [inQ.put(None) for i in range(n_w)]

        completed_files = []
        while len(completed_files) != len(file_list):
            completed_files.append(outQ.get())

        [w.join() for w in workers]

1条回答
Luminary・发光体
2楼-- · 2019-07-25 11:49

Guess what...

implement the following to build the GUI only once:

    import multiprocess as mp
    from PyQt5 import QtWidgets
    from QtWidgets import QMainWindow

    ... snippet ... your script code ...

    if __name__ == '__main__':

        app = QtWidgets.QApplication(sys.argv)
        window = MyApp()
        window.setGeometry(300, 100, 600, 600)   # adjustable (left,top,width,height)
        window.show()
        sys.exit(app.exec_())

... you probably figured this one out by now ;p

查看更多
登录 后发表回答