对于我基于 Python 和 Qt 的项目,我想将昂贵的计算和提供服务器/客户端功能的函数转移到单独的线程中,以解冻我的 GUI。在让它们运行的同时,我仍然希望它们定期检查主线程是否有新数据。为了进行测试,我实现了以下演示代码:
import sys
from time import sleep
import shiboken6
from PySide6.QtCore import Qt, QObject, QThread, Signal, Slot, QTimer
from PySide6.QtWidgets import (
QApplication,
QLabel,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
class Worker(QObject):
finished = Signal()
progress = Signal(int)
def __init__(self):
super().__init__()
self.print_to_console_plz = False
@Slot()
def print_on_console_while_running(self):
self.print_to_console_plz = True
print("Set print_to_console to true")
def run(self):
timer = QTimer()
for i in range(5):
sleep(0.9)
timer.start(100)
if self.print_to_console_plz:
print("Hello World from worker")
self.print_to_console_plz = False
self.progress.emit(i + 1)
self.finished.emit()
class Window(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.clicksCount = 0
self.initWorker()
self.setupUi()
def initWorker(self):
self.thread = QThread()
# Step 3: Create a worker object
self.worker = Worker()
# Step 4: Move worker to the thread
self.worker.moveToThread(self.thread)
# Step 5: Connect signals and slots
self.thread.started.connect(self.worker.run)
self.worker.finished.connect(self.thread.quit)
self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.worker.progress.connect(self.reportProgress)
def setupUi(self):
self.setWindowTitle("Freezing GUI")
self.resize(300, 150)
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
# Create and connect widgets
self.clicksLabel = QLabel("Counting: 0 clicks", self)
self.clicksLabel.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.stepLabel = QLabel("Long-Running Step: 0")
self.stepLabel.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.clicksToConsoleLabel = QLabel("Click here to print to console", self)
self.clicksToConsoleLabel.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.countBtn = QPushButton("Click me!", self)
self.countBtn.clicked.connect(self.countClicks)
self.ToConsoleBttn = QPushButton("Print to console!", self)
self.ToConsoleBttn.clicked.connect(self.worker.print_on_console_while_running)
self.longRunningBtn = QPushButton("Long-Running Task!", self)
self.longRunningBtn.clicked.connect(self.runLongTask)
# Set the layout
layout = QVBoxLayout()
layout.addWidget(self.clicksLabel)
layout.addWidget(self.countBtn)
layout.addStretch()
layout.addWidget(self.clicksToConsoleLabel)
layout.addWidget(self.ToConsoleBttn)
layout.addStretch()
layout.addWidget(self.stepLabel)
layout.addWidget(self.longRunningBtn)
self.centralWidget.setLayout(layout)
def countClicks(self):
self.clicksCount += 1
self.clicksLabel.setText(f"Counting: {self.clicksCount} clicks")
def reportProgress(self, n):
self.stepLabel.setText(f"Long-Running Step: {n}")
def runLongTask(self):
"""Long-running task in 5 steps."""
# Step 6: Start the thread
if not shiboken6.isValid(self.thread):
self.initWorker()
self.ToConsoleBttn.clicked.connect(self.worker.print_on_console_while_running)
self.thread.start()
# Final resets
self.longRunningBtn.setEnabled(False)
self.thread.finished.connect(
lambda: self.longRunningBtn.setEnabled(True)
)
self.thread.finished.connect(
lambda: self.stepLabel.setText("Long-Running Step: 0")
)
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())
我的主要目标是让“昂贵”的函数在工作线程中运行,同时计数,但它仍然应该定期检查是否有新数据可用(通过调用 print_on_console_while_running
表示) 。为了避免 run 函数在执行时阻塞所有内容,我还引入了 QTimer
作为非阻塞计时器。
无论如何,每当我按下“打印到控制台!”按钮时当工作人员执行运行函数时,我总是在运行函数完成后而不是在执行期间打印“Set print_to_console to true”,这表明运行函数仍然阻止执行其他一切。
我在这里做错了什么,如何在执行运行函数的同时将数据从主线程发送到工作线程?
最佳答案
看待这个问题的另一种方式是,问题是由您的工作线程运行阻塞循环引起的,该循环会停止其自己线程内的所有事件处理。这将影响工作线程内启动的传入排队信号和计时器,它们都需要将事件发布到工作线程的事件队列。因此,您已经有效地将一小部分卡住行为从 gui 线程转移到了工作线程!
(请注意,如果注释掉删除工作线程的信号连接,则任何“将 print_to_console 设置为 true”消息都将被延迟打印,因为工作线程在完成后无法再阻塞自己的线程)。
如果您想继续使用排队信号和线程本地计时器,您可以定期 enforce processing来自阻塞循环内的线程本地事件。为了使计时器(在某种程度上)精确地工作,这显然意味着您必须比计时器计划超时的频率更频繁地执行此操作。所以像这样的事情应该按预期工作:
class Worker(QObject):
finished = Signal()
progress = Signal(int)
def __init__(self):
super().__init__()
self.print_to_console_plz = False
@Slot()
def print_on_console_while_running(self):
self.print_to_console_plz = True
print("Set print_to_console to true")
@Slot()
def timeout_handler(self):
print('timeout:', QTime.currentTime().toString('HH:mm:ss.z'))
def run(self):
timer = QTimer()
timer.start(100)
timer.timeout.connect(self.timeout_handler)
for i in range(50):
sleep(0.05)
QApplication.processEvents()
if self.print_to_console_plz:
print("Hello World from worker")
self.print_to_console_plz = False
self.progress.emit(i + 1)
self.finished.emit()
关于python - 工作线程不响应主线程的槽调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73109805/