使用PySide或PyQt进行工作进程的典型示例

时间:2017-01-11 12:18:59

标签: python pyqt multiprocessing pyside

我一直在寻找从Python创建的Qt GUI管理工作进程的一个很好的例子。我需要尽可能完整,包括报告流程的进度,包括中止流程,包括处理流程中可能出现的错误。

我只找到了一些只完成了部分工作的半成品例子但是当我试图让它们完成时我失败了。我目前的设计分为三层:

1)主要线程中存在GUI,ProcessScheduler控制只有一个工作进程实例正在运行并且可以中止

2)还有另一个线程,我有ProcessObserver实际运行进程并理解来自队列的东西(用于进程间通信),这必须在非GUI线程中保持GUI响应

3)有一个实际的工作进程执行一段代码(我的未来意图是将multiprocessing替换为multiprocesspathos或其他什么可以腌制函数对象,但这不是我当前的问题)并将进度或结果报告给队列

目前我有这个代码段(代码中的print函数仅用于调试,最终将被删除):

import multiprocessing

from PySide import QtCore, QtGui
QtWidgets = QtGui

N = 10000000

# I would like this to be a function object
# but multiprocessing cannot pickle it :(
# so I will use multiprocess in the future
CODE = """
# calculates sum of numbers from 0 to n-1
# reports percent progress of finished work

sum = 0
progress = -1
for i in range(n):
    sum += i
    p = i * 100 // n
    if p > progress:
        queue.put(["progress", p])
        progress = p
queue.put(["result", sum])
"""


class EvalProcess(multiprocessing.Process):

    def __init__(self, code, symbols):
        super(EvalProcess, self).__init__()
        self.code= code
        self.symbols = symbols  # symbols must contain 'queue'

    def run(self):
        print("EvalProcess started")
        exec(self.code, self.symbols)
        print("EvalProcess finished")


class ProcessObserver(QtCore.QObject):
    """Resides in worker thread. Its role is to understand
    to what is received from the process via the queue."""

    progressChanged = QtCore.Signal(float)
    finished = QtCore.Signal(object)

    def __init__(self, process, queue):
        super(ProcessObserver, self).__init__()
        self.process = process
        self.queue = queue

    def run(self):
        print("ProcessObserver started")
        self.process.start()
        try:
            while True:
                # this loop keeps running and listening to the queue
                # even if the process is aborted
                result = self.queue.get()
                print("received from queue:", result)
                if result[0] == "progress":
                    self.progressChanged.emit(result[1])
                elif result[0] == "result":
                    self.finished.emit(result[1])
                    break

        except Exception as e:
            print(e)  # QUESTION: WHAT HAPPENS WHEN THE PROCESS FAILS?
        self.process.join()  # QUESTION: DO I NEED THIS LINE?
        print("ProcessObserver finished")


class ProcessScheduler(QtCore.QObject):
    """Resides in the main thread."""

    sendText = QtCore.Signal(str)

    def __init__(self):
        super(ProcessScheduler, self).__init__()
        self.observer = None
        self.thread = None
        self.process = None
        self.queue = None

    def start(self):
        if self.process:  # Q: IS THIS OK?
            # should kill current process and start a new one
            self.abort()
        self.queue = multiprocessing.Queue()
        self.process = EvalProcess(CODE, {"n": N, "queue": self.queue})
        self.thread = QtCore.QThread()
        self.observer = ProcessObserver(self.process, self.queue)
        self.observer.moveToThread(self.thread)
        self.observer.progressChanged.connect(self.onProgressChanged)
        self.observer.finished.connect(self.onResultReceived)
        self.thread.started.connect(self.observer.run)
        self.thread.finished.connect(self.onThreadFinished)
        self.thread.start()
        self.sendText.emit("Calculation started")

    def abort(self):
        self.process.terminate()
        self.sendText.emit("Aborted.")
        self.onThreadFinished()

    def onProgressChanged(self, percent):
        self.sendText.emit("Progress={}%".format(percent))

    def onResultReceived(self, result):
        print("onResultReceived called")
        self.sendText.emit("Result={}".format(result))
        self.thread.quit()

    def onThreadFinished(self):
        print("onThreadFinished called")
        self.thread.deleteLater()  # QUESTION: DO I NEED THIS LINE?
        self.thread = None
        self.observer = None
        self.process = None
        self.queue = None


if __name__ == '__main__':
    app = QtWidgets.QApplication([])

    scheduler = ProcessScheduler()

    window = QtWidgets.QWidget()
    layout = QtWidgets.QVBoxLayout(window)

    startButton = QtWidgets.QPushButton("sum(range({}))".format(N))
    startButton.pressed.connect(scheduler.start)
    layout.addWidget(startButton)

    abortButton = QtWidgets.QPushButton("Abort")
    abortButton.pressed.connect(scheduler.abort)
    layout.addWidget(abortButton)

    console = QtWidgets.QPlainTextEdit()
    scheduler.sendText.connect(console.appendPlainText)
    layout.addWidget(console)

    window.show()
    app.exec_()

它工作正常但它仍然缺乏正确的错误处理和流程中止。特别是我现在正在努力堕胎。主要问题是工作线程继续运行(在循环中侦听队列),即使进程已在计算过程中被中止/终止(或者至少它在控制台QThread: Destroyed while thread is still running中打印此错误) 。有办法解决这个问题吗?或者任何替代方法?或者,如果可能的话,这种任务的任何现实生活和竞争的例子都能满足上述所有要求吗?任何评论都会非常感激。

0 个答案:

没有答案