U2r*_*ros 5 qt multithreading pyqt signals-slots pyqt4
我正在做一些多线程。我有一个带有work方法的工作类,我将其发送到单独的QThread. 该work方法内部有一个有条件的 while 循环。我希望能够向工作对象发送信号以停止它(将_running条件更改为 false)。这将导致 while 循环退出,并从工作对象(连接到工作线程的退出槽)发送完成信号。
false 条件通过信号发送到工作对象,但从未收到,我认为这是因为 while 循环阻止了其线程的事件循环。即使我放入QCoreApplication.processEvents()while 循环,也没有任何反应。问题出在哪儿?为什么不处理信号?(请注意,Worker 上停止槽中的打印语句从未执行过 - 但奇怪的是,线程似乎以错误的方式停止)。
这是代码:
import time, sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Worker(QObject):
sgnFinished = pyqtSignal()
def __init__(self, parent):
QObject.__init__(self, parent)
self._running = True
@pyqtSlot()
def stop():
print 'stop signal received, switching while loop condition to false'
self._running = False
@pyqtSlot()
def work(self):
while self._running: #this blocks the thread, if changed to an if clause, thread finishes as expected!
QCoreApplication.processEvents() #this doesn't help!
time.sleep(0.1)
print 'doing work...'
#do some cleanup here, then signal the worker is done
self.sgnFinished.emit()
class Client(QObject):
sgnStop = pyqtSignal()
def __init__(self, parent):
QObject.__init__(self, parent)
self._thread = None
self._worker = None
def toggle(self, enable):
if enable:
if not self._thread:
self._thread = QThread()
self._worker = Worker(None)
self._worker.moveToThread(self._thread)
self._worker.sgnFinished.connect(self.on_worker_done)
self.sgnStop.connect(self._worker.stop)
self._thread.started.connect(self._worker.work)
self._thread.start()
else:
print 'sending stop signal to the worker object'
self.sgnStop.emit() #send a queuedconnection type signal to the worker, because its in another thread
@pyqtSlot()
def on_worker_done(self):
print 'workers job was interrupted manually'
self._thread.quit()
#self._thread.wait() not sure this is neccessary
if __name__ == '__main__':
app = QCoreApplication(sys.argv)
client = Client(None)
client.toggle(True)
raw_input('Press something')
client.toggle(False)
Run Code Online (Sandbox Code Playgroud)
您的示例中有两个主要问题:
首先,您发出一个信号来停止工作线程,但由于该信号是跨线程的,因此它将被发布到接收者的事件队列中。但是,工作线程正在运行阻塞 while 循环,因此无法处理挂起的事件。有几种方法可以解决这个问题,但最简单的可能是直接调用工作人员的stop方法而不是使用信号。
其次,您没有在主线程中显式运行事件循环,因此从工作线程发送的跨线程信号无法排队。但更重要的是,在用户按下按键后也没有任何东西可以阻止程序退出 - 因此客户端和工作线程将立即被垃圾收集。
下面是您的示例的重写版本,它修复了所有问题:
import time, sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Worker(QObject):
sgnFinished = pyqtSignal()
def __init__(self, parent):
QObject.__init__(self, parent)
self._mutex = QMutex()
self._running = True
@pyqtSlot()
def stop(self):
print 'switching while loop condition to false'
self._mutex.lock()
self._running = False
self._mutex.unlock()
def running(self):
try:
self._mutex.lock()
return self._running
finally:
self._mutex.unlock()
@pyqtSlot()
def work(self):
while self.running():
time.sleep(0.1)
print 'doing work...'
self.sgnFinished.emit()
class Client(QObject):
def __init__(self, parent):
QObject.__init__(self, parent)
self._thread = None
self._worker = None
def toggle(self, enable):
if enable:
if not self._thread:
self._thread = QThread()
self._worker = Worker(None)
self._worker.moveToThread(self._thread)
self._worker.sgnFinished.connect(self.on_worker_done)
self._thread.started.connect(self._worker.work)
self._thread.start()
else:
print 'stopping the worker object'
self._worker.stop()
@pyqtSlot()
def on_worker_done(self):
print 'workers job was interrupted manually'
self._thread.quit()
self._thread.wait()
if raw_input('\nquit application [Yn]? ') != 'n':
qApp.quit()
if __name__ == '__main__':
# prevent some harmless Qt warnings
pyqtRemoveInputHook()
app = QCoreApplication(sys.argv)
client = Client(None)
def start():
client.toggle(True)
raw_input('Press something\n')
client.toggle(False)
QTimer.singleShot(10, start)
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5449 次 |
| 最近记录: |