所有任务或多个特定工作人员的单个工作线程?

Jam*_*mas 6 python qt multithreading pyqt pyqt5

我正在使用PyQt5创建一个简单的GUI应用程序,我从API请求一些数据,然后用于填充UI的各种控件.

我在PyQt中关于工作线程的示例似乎都是子类QThread,然后在重写run()方法中执行它们的业务逻辑.这很好但我想使用worker在不同的时间执行不同的API调用.

所以我的问题是:我是否需要为我希望做的每个操作创建一个特定的工作线程,或者是否有一种方法可以使用单个线程类来在不同的时间执行不同的操作,从而避免开销创建不同的线程子类?

cod*_*der 9

你可以做的是设计一个对象来完成所有这些任务(为插槽/信号继承QObject).让我们说每个任务都被定义为一个单独的函数 - 让我们将这些函数指定为插槽.

然后(事件的一般顺序):

  • 实例化QThread对象.
  • 实例化你的课程.
  • 使用将对象移动到线程中YouClass->moveToThread(pThread).
  • 现在为每个插槽定义一个信号,并将这些信号连接到对象中的相关插槽.
  • 最后使用运行线程 pThread->start()

现在,您可以发出信号以在线程中执行特定任务.你不需要子类QThread只需使用从QObject派生的普通类(这样你就可以使用插槽/信号).

您可以在一个线程中使用一个类来执行许多操作(注意:它们将排队).或者在许多线程中创建许多类(以"并行"运行).

我不太了解python足以在这里尝试一个例子所以我不会:o

注意:如果你想扩展QThread类的功能 - 即添加更多/特定的线程相关函数,那么子类QThread的原因就是如此.QThread是一个控制线程的类,并不打算用于运行任意/通用任务......即使你可以滥用它,如果你愿意:)

  • 很好的答案,特别是他们暗示这些工作会自动排队.也许我会为此写一个例子. (2认同)

Tri*_*ion 5

下面是一个简洁的示例(可能是您希望的多个)工作对象,它被移动到单个正在运行的QThread(已启动)并通过信号进行通信.线程也在最后停止.它演示了他的答案中概述的code_fodder .

from PyQt4 import QtCore
QtCore.Signal = QtCore.pyqtSignal

class Master(QtCore.QObject):

    command = QtCore.Signal(str)

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

class Worker(QtCore.QObject):

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

    def do_something(self, text):
        print('current thread id = {}, message to worker = {}'.format(int(QtCore.QThread.currentThreadId()), text))

if __name__ == '__main__':

    app = QtCore.QCoreApplication([])

    # give us a thread and start it
    thread = QtCore.QThread()
    thread.start()

    # create a worker and move it to our extra thread
    worker = Worker()
    worker.moveToThread(thread)

    # create a master object and connect it to the worker
    master = Master()
    master.command.connect(worker.do_something)

    # call a method of the worker directly (will be executed in the actual thread)
    worker.do_something('wrong way to communicate with worker')

    # communicate via signals, will execute the method now in the extra thread
    master.command.emit('right way to communicate with worker')

    # start the application and kill it after 1 second
    QtCore.QTimer.singleShot(1000, app.quit)
    app.exec_()

    # don't forget to terminate the extra thread
    thread.quit()
    thread.wait(5000)
Run Code Online (Sandbox Code Playgroud)