多处理和GUI更新 - Qprocess还是多处理?

时间:2013-03-28 05:46:28

标签: python multithreading qt user-interface multiprocessing

在阅读有关QProcesses的文献和python的多处理模块之后,我仍然无法在后台持续进行大型流程的过程中创建工作和响应式GUI。 到目前为止,我已经提出了我的应用程序的这个简化版本,它仍然显示出与许多人描述的类似的问题。

from PyQt4 import QtCore, QtGui
import multiprocessing as mp
import numpy as np
import sys
class Spectra:
    def __init__(self, spectra_name, X, Y):
        self.spectra_name = spectra_name
        self.X = X
        self.Y = Y
        self.iteration = 0

    def complex_processing_on_spectra(self, pipe_conn):
        self.iteration += 1
        pipe_conn.send(self.iteration)

class Spectra_Tab(QtGui.QTabWidget):
    def __init__(self, parent, spectra):
        self.parent = parent
        self.spectra = spectra
        QtGui.QTabWidget.__init__(self, parent)

        self.treeWidget = QtGui.QTreeWidget(self)
        self.properties = QtGui.QTreeWidgetItem(self.treeWidget, ["Properties"])
        self.step = QtGui.QTreeWidgetItem(self.properties, ["Iteration #"])

        self.consumer, self.producer = mp.Pipe()
        # Make process associated with tab
        self.process = mp.Process(target=self.spectra.complex_processing_on_spectra, args=(self.producer,))

    def update_GUI(self, iteration):
        self.step.setText(1, str(iteration))

    def start_computation(self):
        self.process.start()
        while(True):
            message = self.consumer.recv()
            if message == 'done':
                break
            self.update_GUI(message)
        self.process.join()
        return

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self)

        self.setTabShape(QtGui.QTabWidget.Rounded)
        self.centralwidget = QtGui.QWidget(self)
        self.top_level_layout = QtGui.QGridLayout(self.centralwidget)

        self.tabWidget = QtGui.QTabWidget(self.centralwidget)
        self.top_level_layout.addWidget(self.tabWidget, 1, 0, 25, 25)

        process_button = QtGui.QPushButton("Process")
        self.top_level_layout.addWidget(process_button, 0, 1)
        QtCore.QObject.connect(process_button, QtCore.SIGNAL("clicked()"), self.process)

        self.setCentralWidget(self.centralwidget)
        self.centralwidget.setLayout(self.top_level_layout)

        # Open several files in loop from button - simplifed to one here
        X = np.arange(0.1200,.2)
        Y = np.arange(0.1200,.2)
        self.spectra = Spectra('name', X, Y)
        self.spectra_tab = Spectra_Tab(self.tabWidget, self.spectra)
        self.tabWidget.addTab(self.spectra_tab, 'name')

    def process(self):
        self.spectra_tab.start_computation()
        return

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

如果你有依赖项,这应该完全能够执行。 目前我有一个QThreaded版本的程序,可以处理信号和插槽;但是,我认为能够使用所有计算机处理器非常重要,因为大多数用户可以使用~8个核心。所以,我想使用multiprocessingQProcess将这种信号/槽线程方法扩展到多处理版本。
有没有人建议是否使用QProcessmultiprocessing?虽然它们对我来说都很复杂,但QProcess看起来似乎没有人使用pyQt的论坛,所以我选择了多处理。使用QProcess会更简单,因为我已经有信号/插槽使用线程吗?

编辑: 我应该按建议添加这样的课程吗?

class My_Process(QtCore.QProcess):
    def __init__(self, spectra):
        QtCore.QProcess.__init__(self)
        self.spectra = spectra

    def worker(self):
        QtConcurrent.run(self.spectra, self.spectra.complex_processing_on_spectra)

    def run(self):
        QtCore.QObject.connect(self, QtCore.SIGNAL(QTimer.timeout()), self.worker)

2 个答案:

答案 0 :(得分:20)

即使这个问题已经过时并且已经得到解答,我想补充一些说明:

  • PyQt中没有AFAIK QtConcurrent
  • Qt中使用C ++的线程与PyQt或python线程不同。后者需要在运行时获取python的GIL(全局解释器锁),这实际上意味着python / pyqt线程之间没有真正的并发。

Python线程可以保持gui响应,但是你不会看到cpu绑定任务的任何性能改进。我建议在主进程上使用multiprocessingQThread来处理与子进程的通信。您可以在主(gui)和通信线程之间使用信号和插槽。

enter image description here

编辑:我遇到了同样的问题并按照以下方式做了一些事情:

from multiprocessing import Process, Queue
from PyQt4 import QtCore
from MyJob import job_function


# Runner lives on the runner thread

class Runner(QtCore.QObject):
    """
    Runs a job in a separate process and forwards messages from the job to the
    main thread through a pyqtSignal.

    """

    msg_from_job = QtCore.pyqtSignal(object)

    def __init__(self, start_signal):
        """
        :param start_signal: the pyqtSignal that starts the job

        """
        super(Runner, self).__init__()
        self.job_input = None
        start_signal.connect(self._run)

    def _run(self):
        queue = Queue()
        p = Process(target=job_function, args=(queue, self.job_input))
        p.start()
        while True:
            msg = queue.get()
            self.msg_from_job.emit(msg)
            if msg == 'done':
                break


# Things below live on the main thread

def run_job(input):
    """ Call this to start a new job """
    runner.job_input = input
    runner_thread.start()


def handle_msg(msg):
    print(msg)
    if msg == 'done':
        runner_thread.quit()
        runner_thread.wait()


# Setup the OQ listener thread and move the OQ runner object to it
runner_thread = QtCore.QThread()
runner = Runner(start_signal=runner_thread.started)
runner.msg_from_job.connect(handle_msg)
runner.moveToThread(runner_thread)

答案 1 :(得分:2)

QProcess用于读取和写入管道(shell / cmd)。使用其中之一。

  1. 使用辅助功能创建一个类,并按QtConcurrent.run(object, method, args)
  2. 运行它作为线程
  3. QTimer.timeout()与您的工作人员联系起来。