python - 这段代码中真的需要互斥锁吗?

标签 python pyqt mutex

下面的代码显示了我用于 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"

最佳答案

我会把它留在原处。
如果您不幸的话,您的重置/增量是由一个线程在倒计时线程读取计数器之后和写回其减量结果之前的小时间窗口中执行的。然后它会覆盖更新的值,甚至看不到它。
这个时间窗口非常小,因此它可能不太可能发生,但有可能。

顺便说一下,使用锁的首选方式是:

def increment(self, seconds):
    with self._mutex:
        self._countdownInSec += seconds

关于python - 这段代码中真的需要互斥锁吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30125318/

相关文章:

c++ - static const char [] 在类线程安全吗?

python - 假设检验 : how to sample_from values from another strategy?

Python:Python WX GUI 应用程序从控制台窃取焦点

pyqt - 键/值 pyqt QComboBox

qt - 在Qt4中,如何检查paintEvent是否由resize触发?

c - 多个生产者中的 posix 线程互斥和条件变量使用

linux - 如果我在创建子进程之前初始化一个互斥量,所有的子进程都会接受这个互斥量吗?

python - 是否有 Python 的 Cake 等价物?

python - 构建 Sphinx 文档

python - PyQt 中的动态小部件添加