gpt4 book ai didi

python - 线程和条件 : debugging the acquirement

转载 作者:行者123 更新时间:2023-11-28 16:31:32 27 4
gpt4 key购买 nike

我的一个程序中有一个奇怪的问题,一个线程获取一个条件,而另一个线程告诉我没有获取该条件。

为了知道线程是否获得了条件,我做了一些调试信息,看起来是的,他做到了。但是另一个线程告诉我条件没有获得。

这是关于我是如何做到的以及我得到的输出的总结(不是很短,对此感到抱歉):

import logging
import time
from threading import Condition, Lock, Thread
logging.basicConfig(level=logging.DEBUG)

class MyClass:

def __init__(self):

self._is_alive = False
self._thread_update = None
self._conditioned_thread = None
self._thread_condition = Condition(Lock()) # Or RLock() or nothing, same issue

def start(self):

self._is_alive = True
self._thread_update = Thread(target=self._loop_update, name="Updater")
self._conditioned_thread = Thread(target=self._loop_conditioned, name="Conditioned")
self._thread_update.start()
self._conditioned_thread.start()

def _loop_conditioned(self):
logging.debug("Starting conditioned thread")

with self._thread_condition:
while self._is_alive:
logging.debug("Awaiting... Is acquired ? %s", self._thread_condition._is_owned())
self._thread_condition.wait()
logging.debug("Success !")

def _loop_update(self):
time.sleep(1)
logging.debug("Notifying ! Is acquired ? %s", self._thread_condition._is_owned())
self._thread_condition.notify(1)
# Do some stuff

def stop(self):

self._is_alive = False
self._thread_condition.notify()
self._thread_update.join()
self._thread_condition.join()

if __name__ == "__main__":
c = MyClass()
c.start()
time.sleep(4)
c.stop()

这是输出:

DEBUG:root:Starting conditioned thread
DEBUG:root:Awaiting... Is acquired ? True
DEBUG:root:Notifying ! Is acquired ? False
Exception in thread Updater:
Traceback (most recent call last):
File "/usr/lib64/python2.6/threading.py", line 532, in __bootstrap_inner
self.run()
File "/usr/lib64/python2.6/threading.py", line 484, in run
self.__target(*self.__args, **self.__kwargs)
File ".../test.py", line 39, in _loop_update
self._thread_condition.notify(1)
File "/usr/lib64/python2.6/threading.py", line 274, in notify
raise RuntimeError("cannot notify on un-acquired lock")
RuntimeError: cannot notify on un-acquired lock

Traceback (most recent call last):
File ".../test.py", line 53, in <module>
c.stop()
File ".../test.py", line 45, in stop
self._thread_condition.notify()
File "/usr/lib64/python2.6/threading.py", line 274, in notify
raise RuntimeError("cannot notify on un-acquired lock")
RuntimeError: cannot notify on un-acquired lock

问题是为什么我有 RuntimeError,即使我已经获得了条件?

最佳答案

我不是线程专家,但是 documentation有这样的话:

wait(timeout=None)

Wait until notified or until a timeout occurs. Ifthe calling thread has not acquired the lock when this method iscalled, a RuntimeError is raised.

This method releases the underlying lock, and then blocks until it isawakened by a notify() or notify_all() call for the same conditionvariable in another thread, or until the optional timeout occurs. Onceawakened or timed out, it re-acquires the lock and returns.

(强调我的)。

因此,调用 wait() 似乎释放了 _conditioned_thread 线程获取的锁。可能,该锁然后需要以某种方式被另一个线程获取(可能在循环中检查锁是否可用);然后,另一个线程可以通知(1)另一个线程继续(并重新获取锁,按照上面的最后一句话)。

关于python - 线程和条件 : debugging the acquirement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31431484/

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