我有一些计算量大的任务,我想每5秒在一个循环中运行一次,而又不会阻塞主事件循环。为此,我打算使用QTimer
和一个单独的线程来运行它。我已经尝试了以下代码,但到目前为止尚未奏效:
@pyqtSlot()
def heavy_task_function():
# Sleep for 10 seconds to simulate heavy computation
time.sleep(10)
print "First Timer Fired"
if __name__ == "__main__":
app = QCoreApplication.instance()
if app is None:
app = QApplication(sys.argv)
threaded_timer = ModbusComThread(heavy_task_function)
threaded_timer.start()
sys.exit(app.exec_())
位置:
class ModbusComThread(QThread):
def __init__(self, slot_function):
QThread.__init__(self)
self.slot_function = slot_function
self.send_data_timer = None
def run(self):
print "Timer started on different thread"
self.send_data_timer = QTimer(self)
self.send_data_timer.timeout.connect(self.slot_function)
self.send_data_timer.start(5000)
def stop(self):
self.send_data_timer.stop()
slot_function
中的QTimer
不会触发threaded_timer
。我的线程架构正确吗?
答案 0 :(得分:2)
QTimer
需要一个运行中的事件循环。默认情况下,QThread.run()
将为线程启动一个本地事件循环,但是如果您以完成的方式完全覆盖它,则不会发生-因此将永远不会处理计时器事件。>
通常,当您需要局部事件循环时,应创建一个辅助对象来执行所有处理,然后使用moveToThread将其放在单独的线程中。如果没有,覆盖QThread.run()
是完全可以的。
下面的演示显示了如何执行此操作。请注意,在线程启动后创建计时器非常重要,否则它将在错误的线程中创建,并且其计时器事件不会被线程的事件循环处理。同样重要的是,工作线程与主线程之间的所有通信均通过信号进行,以确保线程安全。切勿尝试直接在主线程之外直接执行GUI操作,因为Qt根本不支持该操作。出于演示目的,在固定间隔后,主线程中的第二个计时器用于停止所有处理。如果有GUI,则用户通过按钮进行干预将实现相同的目的。
演示:
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class ModbusComWorker(QObject):
finished = pyqtSignal()
def start(self):
self._timer = QTimer(self)
self._timer.timeout.connect(self.process)
self._timer.start(2000)
def stop(self):
self._timer.stop()
self.finished.emit()
def process(self):
print('processing (thread: %r)' % QThread.currentThread())
QThread.sleep(3)
if __name__ == "__main__":
app = QCoreApplication.instance()
if app is None:
app = QApplication(sys.argv)
thread = QThread()
worker = ModbusComWorker()
worker.moveToThread(thread)
def finish():
print('shutting down...')
thread.quit()
thread.wait()
app.quit()
print('stopped')
worker.finished.connect(finish)
thread.started.connect(worker.start)
thread.start()
timer = QTimer()
timer.setSingleShot(True)
timer.timeout.connect(worker.stop)
timer.start(15000)
print('starting (thread: %r)' % QThread.currentThread())
sys.exit(app.exec_())
输出:
starting (thread: <PyQt5.QtCore.QThread object at 0x7f980d096b98>)
processing (thread: <PyQt5.QtCore.QThread object at 0x7f980d0968a0>)
processing (thread: <PyQt5.QtCore.QThread object at 0x7f980d0968a0>)
processing (thread: <PyQt5.QtCore.QThread object at 0x7f980d0968a0>)
processing (thread: <PyQt5.QtCore.QThread object at 0x7f980d0968a0>)
processing (thread: <PyQt5.QtCore.QThread object at 0x7f980d0968a0>)
shutting down...
stopped