PyQt 5 中的多线程 [英] Multi-Threading in PyQt 5

查看:444
本文介绍了PyQt 5 中的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习多线程,特别是在 PyQt 5 应用程序的上下文中.

最初我使用线程"实现了一个版本,但后来了解到我应该使用QThread"来允许使用信号/插槽,例如:

<块引用>

workerThread = QThread()
workerObject = Worker(cmdlist)
workerObject.moveToThread(workerThread)
workerThread.started.connect(workerObject.run)
workerObject.finished.connect(workerThread.quit)

但是,是否可以设计一个系统,其中:

  • 每个类都与一个在运行时创建的线程相关联.
  • 程序的主要"组件然后可以调用这些组件中的函数类,它们在给定的单独线程中执行类.

行为的一个例子是这样的:

<块引用>

thread = threading.Thread(target=self.run, args=())

但是我将如何使用 QThread 实现类似的行为?
还是我对 Python 中线程的理解不正确?

解决方案

Martin Fitzpatrick 有一个很棒的指南关于如何使用 QThreadPools 做到这一点.我想这就是你要找的.

使用 QThreadPool 处理多线程 PyQt 应用程序

I've been learning about multi-threading, specifically in the context of a PyQt 5 application.

Initially I implemented a version using 'threading', but have since learnt that I should be using 'QThread' to allow use of signals / slots, e.g:

workerThread = QThread()
workerObject = Worker(cmdlist)
workerObject.moveToThread(workerThread)
workerThread.started.connect(workerObject.run)
workerObject.finished.connect(workerThread.quit)

However, is it possible to design a system in which:

  • Each class is associated with a thread created at run-time.
  • The'main' component of the program can then call functions within those classes, which are executed within the separate thread for the given class.

An example of the behaviour would be this:

thread = threading.Thread(target=self.run, args=())

But how would I implement similar behaviour with QThread?
Or my understanding of threads in Python in-correct?

解决方案

Martin Fitzpatrick has an amazing guide on how to do this using QThreadPools. I think this is what you're looking for.

Multithreading PyQt applications with QThreadPool

这篇关于PyQt 5 中的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆