如何通过多线程任务发出pyqtsignal

时间:2019-05-24 00:51:21

标签: python pyqt pyqt5 concurrent.futures

我正在尝试通过多线程方式发出pyqtsignal。我创建了一个执行计算的函数(例如func)。另一个承担该任务并在多个线程中运行的函数(例如Function)。

当我使用父线程时,代码运行良好。但是,当我使用多个线程时,计算效果很好,但是没有发出信号。

我需要使用多线程,因为我正在编写的功能执行计算量大的任务。

请在下面的示例代码中找到(在此示例中,我使用了简单的打印功能)

from PyQt5.QtCore import QObject, pyqtSignal,pyqtSlot
import time
from threading import Thread
import sys
import math
import concurrent.futures


class Plot2D(QObject):
    finish=pyqtSignal(float)

    def __init__(self):
        super(Plot2D,self).__init__()

    def Function(self):
        st = time.time()

        # Using parent thread
        # self.func()

        # Using multi-thread 1
        #t=Thread(target=self.func)
        #t.start()
        #t.join()

        # Using multi-thread 2
        with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
           f = executor.submit(self.func)
        en = time.time()
        print(en-st)

    def func(self):
        n=10
        v=(1*100/(n-1))

        for i in range(n):
            print('thread')
            self.finish.emit(v)

    def fprint(self):
        print('works')

obj=Plot2D()
obj.finish.connect(obj.fprint)
obj.Function()

1 个答案:

答案 0 :(得分:0)

您必须清楚以下概念:信号需要事件循环才能起作用。

考虑到上述情况,解决方案是:

  • threading.Thread

您不应使用join(),因为它会阻塞事件循环所在的主线程,并且由于上述原因,信号将无法工作。

from PyQt5 import QtCore
from threading import Thread


class Plot2D(QtCore.QObject):
    finished = QtCore.pyqtSignal(float)

    def Function(self):
        Thread(target=self.func).start()

    def func(self):
        n = 10
        v = 1 * 100 / (n - 1)

        for i in range(n):
            print("thread")
            self.finished.emit(v)

    @QtCore.pyqtSlot()
    def fprint(self):
        print("works")


if __name__ == "__main__":
    import sys

    app = QtCore.QCoreApplication(sys.argv)
    obj = Plot2D()
    obj.finished.connect(obj.fprint)
    obj.Function()
    sys.exit(app.exec_())

输出:

thread
thread
thread
works
thread
works
thread
works
thread
thread
works
thread
works
thread
thread
works
works
works
works
works
  • concurrent.futures

不要使用with,因为它将使执行程序阻塞主线程(并且我们已经知道它会产生什么问题),它还会调用executor.shutdown(wait = False)

from PyQt5 import QtCore
import concurrent.futures


class Plot2D(QtCore.QObject):
    finished = QtCore.pyqtSignal(float)

    def Function(self):
        executor = concurrent.futures.ThreadPoolExecutor(max_workers=3)
        f = executor.submit(self.func)
        executor.shutdown(wait=False)

    def func(self):
        n = 10
        v = 1 * 100 / (n - 1)

        for i in range(n):
            print("thread")
            self.finished.emit(v)

    @QtCore.pyqtSlot()
    def fprint(self):
        print("works")


if __name__ == "__main__":
    import sys

    app = QtCore.QCoreApplication(sys.argv)
    obj = Plot2D()
    obj.finished.connect(obj.fprint)
    obj.Function()
    sys.exit(app.exec_())

输出:

thread
thread
works
thread
works
thread
works
thread
works
thread
thread
works
thread
works
thread
works
thread
works
works
works
相关问题