gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:38 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com