python 个锁问题
A python issue with locks
我有一段代码试图暂停特定操作。我有一种暂停应用程序的方法和另一种在特定超时值后恢复应用程序的方法。为了实现这一点,我有一个运行固定时间间隔的定时器线程。
像这样考虑下面的方法-
def pause_my_operation():
with self._lock:
# check if there is already an existing timer, if present then cancel the timer and start a new timer
# pause the operation
def pausetimeout():
with self._lock:
# check if there is already an existing timer, if present then cancel it.
# resume the operation
操作在 UI 中有两个地方可以暂停。因此,在暂停方法中检查计时器。
现在,我面临的问题是这两个之间可能存在竞争 functions.If 第一个暂停提前一段时间触发,即将过期,即,第一个暂停的 pausetimeout 刚刚进入方法,但在获得锁之前,UI 有第二次调用暂停操作,即调用 pause_my_operation 并获得锁。第二个 pause_my_operation 将简单地设置一个内部事件来标记计时器已取消,但这可能不会阻止暂停超时继续进行,因为它已经被服务。因此,第二次暂停调用不会产生任何影响,并且它的计时器会被第一次暂停的超时调用取消。
知道如何解决这个问题吗?
您可以创建一个递增 pause_my_operation()
并递减 pausetimeout()
的变量。然后,pausetimeout()
只会在递减变量为 0 后执行其逻辑。使用此逻辑,只有最后一个 pausetimeot()
会恢复代码。
例如:
def pause_my_operation():
with self._lock:
self._counter += 1
# check if there is already an existing timer, if present then cancel the timer and start a new timer
# pause the operation
def pausetimeout():
with self._lock:
self._counter -= 1
if self._counter == 0:
# check if there is already an existing timer, if present then cancel it.
# resume the operation
编辑
显然,这样您会遇到另一个问题:如果您取消计时器而不减少该值,那么清理代码将永远不会触发。要解决这个问题,如果可能的话,你永远不应该取消旧计时器,即:
def pause_my_operation():
with self._lock:
self._counter += 1
# start a new timer
# pause the operation
def pausetimeout():
with self._lock:
self._counter -= 1
if self._counter == 0:
# resume the operation
这应该不会影响性能,因为一次几乎总是只有一个计时器。
我有一段代码试图暂停特定操作。我有一种暂停应用程序的方法和另一种在特定超时值后恢复应用程序的方法。为了实现这一点,我有一个运行固定时间间隔的定时器线程。
像这样考虑下面的方法-
def pause_my_operation():
with self._lock:
# check if there is already an existing timer, if present then cancel the timer and start a new timer
# pause the operation
def pausetimeout():
with self._lock:
# check if there is already an existing timer, if present then cancel it.
# resume the operation
操作在 UI 中有两个地方可以暂停。因此,在暂停方法中检查计时器。
现在,我面临的问题是这两个之间可能存在竞争 functions.If 第一个暂停提前一段时间触发,即将过期,即,第一个暂停的 pausetimeout 刚刚进入方法,但在获得锁之前,UI 有第二次调用暂停操作,即调用 pause_my_operation 并获得锁。第二个 pause_my_operation 将简单地设置一个内部事件来标记计时器已取消,但这可能不会阻止暂停超时继续进行,因为它已经被服务。因此,第二次暂停调用不会产生任何影响,并且它的计时器会被第一次暂停的超时调用取消。
知道如何解决这个问题吗?
您可以创建一个递增 pause_my_operation()
并递减 pausetimeout()
的变量。然后,pausetimeout()
只会在递减变量为 0 后执行其逻辑。使用此逻辑,只有最后一个 pausetimeot()
会恢复代码。
例如:
def pause_my_operation():
with self._lock:
self._counter += 1
# check if there is already an existing timer, if present then cancel the timer and start a new timer
# pause the operation
def pausetimeout():
with self._lock:
self._counter -= 1
if self._counter == 0:
# check if there is already an existing timer, if present then cancel it.
# resume the operation
编辑
显然,这样您会遇到另一个问题:如果您取消计时器而不减少该值,那么清理代码将永远不会触发。要解决这个问题,如果可能的话,你永远不应该取消旧计时器,即:
def pause_my_operation():
with self._lock:
self._counter += 1
# start a new timer
# pause the operation
def pausetimeout():
with self._lock:
self._counter -= 1
if self._counter == 0:
# resume the operation
这应该不会影响性能,因为一次几乎总是只有一个计时器。