PyQt5 和多处理 - 多个进程错误地创建了多个窗口 [英] PyQt5 and multiprocessing - multiple processes create multiple windows incorrectly

查看:94
本文介绍了PyQt5 和多处理 - 多个进程错误地创建了多个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在 PyQt5 GUI 框架内实现多处理以在并行进程中处理多个文件,最好在 QThread() 后台.运行下面的代码似乎创建了多个进程,但还有一个额外的问题,即出现多个 GUI 窗口,并且此时一切似乎都被锁定了.

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]

推荐答案

猜猜看...

执行以下操作以仅构建一次 GUI:

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

...你现在可能已经想通了;p

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

这篇关于PyQt5 和多处理 - 多个进程错误地创建了多个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆