gpt4 book ai didi

python - 如何让 QThreads 在控制台 PySide 程序中工作?

转载 作者:太空宇宙 更新时间:2023-11-04 09:15:26 24 4
gpt4 key购买 nike

我正在尝试学习如何在 Python 程序中使用线程。我正在使用 PySide 和 QThreads,因为之后我将使用 PySide 实现 gui。

我已经理解了线程的主要概念,至少我认为是这样。但我仍然对事件循环感到困惑。我认为这是我的应用程序的问题。

这是一个我无法正常工作的示例应用程序。在我的主类中,我有几个工作线程,我想让他们向主类报告他们的进度。但是主程序不会实时打印进度消息。

我怎样才能让它发挥作用?

from PySide import QtCore
import time, sys

class MyWorkerThread(QtCore.QThread):
message = QtCore.Signal(str)

def __init__(self, id, parent=None):
super(MyWorkerThread, self).__init__(parent)
self.id = id

def run(self):
for i in range(10):
self.message.emit("%d: %d" % (self.id, i))
time.sleep(0.2)

class MainProgram():
def __init__(self, parent=None):
self.threads = []

self.addWorker(MyWorkerThread(1))
self.addWorker(MyWorkerThread(2))

def addWorker(self, worker):
worker.message.connect(self.printMessage, QtCore.Qt.QueuedConnection)
self.threads.append(worker)

def startWorkers(self):
for worker in self.threads:
worker.start()
worker.wait()
self.workersFinished()

def workersFinished(self):
QtCore.QCoreApplication.instance().quit()

@QtCore.Slot(str)
def printMessage(self, text):
sys.stdout.write(text+'\n')
sys.stdout.flush()

if __name__ == '__main__':
app = QtCore.QCoreApplication(sys.argv)
m = MainProgram()
m.startWorkers()
sys.exit(app.exec_())

最佳答案

worker.wait() 是问题所在。此调用会阻塞主线程(在本例中为一个正在运行的事件循环),直到工作人员完成其工作。

这是一个稍微改动过的版本(我已经评论了我的改动):

from PySide import QtCore
import time, sys

class MyWorkerThread(QtCore.QThread):
message = QtCore.Signal(str)

def __init__(self, id, parent=None):
super(MyWorkerThread, self).__init__(parent)
self.id = id

def run(self):
for i in range(10):
self.message.emit("%d: %d" % (self.id, i))
time.sleep(0.2)

class MainProgram():
def __init__(self, parent=None):
self.threads = []

self.addWorker(MyWorkerThread(1))
self.addWorker(MyWorkerThread(2))

def addWorker(self, worker):
worker.message.connect(self.printMessage, QtCore.Qt.QueuedConnection)
# connect the finished signal to method so that we are notified
worker.finished.connect(self.workersFinished)
self.threads.append(worker)

def startWorkers(self):
for worker in self.threads:
worker.start()
# no wait, no finished. you start the threads and leave.

def workersFinished(self):
if all(worker.isFinished() for worker in self.threads):
# wait until all the threads finished
QtCore.QCoreApplication.instance().quit()

@QtCore.Slot(str)
def printMessage(self, text):
sys.stdout.write(text+'\n')
sys.stdout.flush()

if __name__ == '__main__':
app = QtCore.QCoreApplication(sys.argv)
m = MainProgram()
m.startWorkers()
sys.exit(app.exec_())

关于python - 如何让 QThreads 在控制台 PySide 程序中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10044197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com