下面的代码显示了我用于PyQt线程功能的类。在我的主程序中,我实例化并启动该线程(即我使用PyQt的moveToThread,而countdown
是run方法。)。在线程运行时,我经常调用reset
方法。我不确定我实施的互斥锁是否真的有必要,我希望有人可以为我清除这一点。我已经执行了一个快速测试,其中我已经注释了互斥锁,这给了我没有问题,即使我注释掉time.sleep(1)
并在线程运行时调用reset
方法没有任何问题延迟。但是,我不是100%安全,因此我的问题在这里。
import time
from PyQt4.QtCore import *
from threading import Lock
class Countdown(QObject):
finished = pyqtSignal()
def __init__(self, countdownInSec=30):
super(Countdown, self).__init__()
self.COUNTDOWN_IN_SEC = countdownInSec
self._countdownInSec = self.COUNTDOWN_IN_SEC
self._mutex = Lock()
@pyqtSlot()
def countdown(self):
while self._countdownInSec > 0:
print(self._countdownInSec)
self._mutex.acquire()
try:
self._countdownInSec -= 1
finally:
self._mutex.release()
time.sleep(1)
self.finished.emit()
def increment(self, seconds):
self._mutex.acquire()
try:
self._countdownInSec += seconds
finally:
self._mutex.release()
def reset(self):
self._mutex.acquire()
try:
self._countdownInSec = self.COUNTDOWN_IN_SEC
finally:
self._mutex.release()
提取主要(仅与该问题相关的部分)
fpIntervUpdCountdownReset = pyqtSignal()
def __init__(self):
self.initFlightPathIntervUpdater()
def initFlightPathIntervUpdater(self):
self.fpIntervUpdCountdownThr = QThread()
self.fpIntervUpdCountdown = countdown.Countdown()
self.fpIntervUpdCountdown.moveToThread(self.fpIntervUpdCountdownThr)
self.fpIntervUpdCountdown.finished.connect(self.fpIntervUpdCountdownThr.quit)
self.fpIntervUpdCountdown.finished.connect(self.flightPathIntervUpdate)
self.fpIntervUpdCountdownThr.started.connect(self.fpIntervUpdCountdown.countdown)
def flightPathIntervUpdateReq(self):
if self.fpIntervUpdCountdownThr.isRunning():
self.fpIntervUpdCountdown.reset()
else:
print 'start'
self.fpIntervUpdCountdownThr.start()
@pyqtSlot()
def flightPathIntervUpdate(self):
print "perform flightPathIntervUpdate"
答案 0 :(得分:1)
我会把它放在原处 如果你不幸,你的复位/增量是在倒计时线程读取计数器之后和回写其递减结果之前的小时间窗口中由一个线程执行的。然后它会覆盖更新后的值,甚至不会看到它 那个时间窗口非常小,所以可能不太可能发生,但这是可能的。
顺便说一句,使用锁的首选方法是:
def increment(self, seconds):
with self._mutex:
self._countdownInSec += seconds