Multi-Threading in PyQt 5

孤街浪徒 提交于 2020-05-29 08:43:15

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/45211218/multi-threading-in-pyqt-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!