PyQt 5 中的多线程 [英] Multi-Threading in 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 做到这一点.我想这就是你要找的.p>
使用 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屋!