PyQt:moveToThread在使用partial()作为插槽时不起作用

时间:2014-04-26 22:30:04

标签: python multithreading qt pyqt pyqt4

我正在构建一个运行生产者(工作者)的小型GUI应用程序,GUI按需消耗输出并绘制它(使用pyqtgraph)。

由于生产者是一个阻塞函数(运行需要一段时间),我(据说)将它移动到自己的线程。

从生产者调用QThread.currentThreadId()时,它输出与主GUI线程相同的编号。因此,首先执行worker,然后执行所有绘图函数调用(因为它们在同一线程的事件队列中排队)。我该如何解决这个问题?

使用partial运行示例:

gui thread id 140665453623104
worker thread id: 140665453623104

这是我的完整代码:

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import pyqtSignal
import pyqtgraph as pg
import numpy as np

from functools import partial
from Queue import Queue
import math
import sys
import time


class Worker(QtCore.QObject):

    termino = pyqtSignal()

    def __init__(self, q=None, parent=None):
        super(Worker, self).__init__(parent) 
        self.q = q

    def run(self, m=30000):
        print('worker thread id: {}'.format(QtCore.QThread.currentThreadId()))
        for x in xrange(m):
            #y = math.sin(x)
            y = x**2
            time.sleep(0.001) # Weird, plotting stops if this is not present...
            self.q.put((x,y,y))
        print('Worker finished')

        self.termino.emit()

class MainWindow(QtGui.QWidget):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.q = Queue()
        self.termino = False       

        self.worker = Worker(self.q)
        self.workerThread = None
        self.btn = QtGui.QPushButton('Start worker')
        self.pw = pg.PlotWidget(self)
        pi = self.pw.getPlotItem()
        pi.enableAutoRange('x', True)
        pi.enableAutoRange('y', True)
        self.ge1 = pi.plot(pen='y')
        self.xs = []
        self.ys = []

        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.pw)
        layout.addWidget(self.btn)

        self.resize(400, 400)

    def run(self):

        self.workerThread = QtCore.QThread()
        self.worker.moveToThread(self.workerThread)
        self.worker.termino.connect(self.setTermino)
        # moveToThread doesn't work here
        self.btn.clicked.connect(partial(self.worker.run, 30000))
        # moveToThread will work here
        # assume def worker.run(self): instead of def worker.run(self, m=30000)
        # self.btn.clicked.connect(self.worker.run)
        self.btn.clicked.connect(self.graficar)

        self.workerThread.start()
        self.show()

    def setTermino(self):
        self.termino = True

    def graficar(self):

        if not self.q.empty():
            e1,e2,ciclos = self.q.get()       
            self.xs.append(ciclos)
            self.ys.append(e1)
            self.ge1.setData(y=self.ys, x=self.xs)

        if not self.termino:
            QtCore.QTimer.singleShot(1, self.graficar)

if __name__ == '__main__':

    app = QtGui.QApplication([])
    window = MainWindow()

    QtCore.QTimer.singleShot(0, window.run);
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:6)

问题是Qt尝试根据signal.connect(slot)存在的线程选择连接类型(当你调用slot时)。因为你已经用Q {{ 1}},您连接的插槽位于MainThread(GUI线程)中。您可以覆盖连接类型(作为partial的第二个参数,但这没有帮助,因为connect()创建的方法将始终存在于MainThread中,因此将连接类型设置为{{ 3}}无济于事。

我能看到的唯一方法就是建立一个中继信号,其唯一目的是有效地将没有参数的发射信号(例如来自按钮的点击信号)更改为带有一个参数的信号(您的partial参数)。这样您就不需要使用m包装QThread中的插槽。

代码如下。我在主windows类中创建了一个带有一个名为'relay'的参数(一个int)的信号。按钮partial()信号连接到主窗口类中的方法,此方法有一行代码,用于发出我创建的自定义信号。您可以扩展此方法(clicked)以使整数以relay_signal()(在本例中为500)传递给QThread,从您喜欢的地方开始!

所以这是代码:

m

如果队列中仍有数据,我还修改了from functools import partial from Queue import Queue import math import sys import time class Worker(QtCore.QObject): termino = pyqtSignal() def __init__(self, q=None, parent=None): super(Worker, self).__init__(parent) self.q = q def run(self, m=30000): print('worker thread id: {}'.format(QtCore.QThread.currentThreadId())) for x in xrange(m): #y = math.sin(x) y = x**2 #time.sleep(0.001) # Weird, plotting stops if this is not present... self.q.put((x,y,y)) print('Worker finished') self.termino.emit() class MainWindow(QtGui.QWidget): relay = pyqtSignal(int) def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.q = Queue() self.termino = False self.worker = Worker(self.q) self.workerThread = None self.btn = QtGui.QPushButton('Start worker') self.pw = pg.PlotWidget(self) pi = self.pw.getPlotItem() pi.enableAutoRange('x', True) pi.enableAutoRange('y', True) self.ge1 = pi.plot(pen='y') self.xs = [] self.ys = [] layout = QtGui.QVBoxLayout(self) layout.addWidget(self.pw) layout.addWidget(self.btn) self.resize(400, 400) def run(self): self.workerThread = QtCore.QThread() self.worker.termino.connect(self.setTermino) self.worker.moveToThread(self.workerThread) # moveToThread doesn't work here # self.btn.clicked.connect(partial(self.worker.run, 30000)) # moveToThread will work here # assume def worker.run(self): instead of def worker.run(self, m=30000) #self.btn.clicked.connect(self.worker.run) self.relay.connect(self.worker.run) self.btn.clicked.connect(self.relay_signal) self.btn.clicked.connect(self.graficar) self.workerThread.start() self.show() def relay_signal(self): self.relay.emit(500) def setTermino(self): self.termino = True def graficar(self): if not self.q.empty(): e1,e2,ciclos = self.q.get() self.xs.append(ciclos) self.ys.append(e1) self.ge1.setData(y=self.ys, x=self.xs) if not self.termino or not self.q.empty(): QtCore.QTimer.singleShot(1, self.graficar) if __name__ == '__main__': app = QtGui.QApplication([]) window = MainWindow() QtCore.QTimer.singleShot(0, window.run); sys.exit(app.exec_()) 方法以继续绘制(即使在线程终止后)。我想这可能就是为什么你需要QThread中的graficar,现在也被删除了。

另请注意您在代码中对time.sleep放置位置的评论,现在正确。它应该在将QThread插槽连接到信号的调用之前,并在此堆栈溢出帖子中讨论其原因:Qt.QueuedConnection