简体   繁体   English

如何知道 Python multiprocessing.Lock 是否被释放?

[英]How to know if a Python multiprocessing.Lock is released or not?

>>> l = Lock()
>>> l.acquire()
True
>>> l.release()
>>> l.release()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: semaphore or lock released too many times

throws a ValueError exception.抛出 ValueError 异常。 How can I prevent to release a lock more than once?如何防止多次释放锁? Something like l.is_released() ?像 l.is_released() 之类的东西?

The question is a bit unclear.问题有点不清楚。 You either need to use semaphores instead of locks or check if lock is locked.您要么需要使用信号量而不是锁,要么检查锁是否被锁定。

Python's locks are not the same as locks on .Net, for example.例如,Python 的锁与 .Net 上的锁不同。 Python's Lock once unlocks releases ALL other threads that acquired() on the same lock and blocked for the time being. Python 的锁一旦解锁就会释放所有其他线程,这些线程在同一锁上获得了()并暂时被阻塞。 Any thread can release and all go at the same time.任何线程都可以同时释放和运行。 So, instead of doing second relase, do所以,不要做第二次,做

if l.locked():
    l.release()

If you want "queue" behavior, where only one tread will get ownership of a lock once some other releases, use Semaphore, Event or some other similar class that allows nested locking and queuing behavior.如果您想要“排队”行为,一旦其他一些释放,只有一个踏板将获得锁定的所有权,请使用信号量、事件或其他一些允许嵌套锁定和排队行为的类似类。

It's interesting to note that other languages/loolkits, like .Net, do lock queuing natively, where threads can pile up lock.acquire in order, block and take ownership of the lock object in the order of acquire queue, not release all at once.有趣的是,其他语言/lookkit,如 .Net,在本地进行锁队列,其中线程可以按顺序堆积 lock.acquire,按照获取队列的顺序阻塞和获取锁对象的所有权,而不是一次全部释放.

(Edit: forgot to put parents as in "if l.locked: l.realse()". Corrected the code. Lock.locked is confirmed to be a present method in cPython 2.6.x, 3.x, IronPython 2.6.1) (编辑:忘记把父母放在“if l.locked: l.realse()”中。更正了代码。Lock.locked 被确认是 cPython 2.6.x、3.x、IronPython 2.6.1 中的现有方法)

The expectation is that the context that acquired the lock should be aware of when it should release it.期望是获取锁的上下文应该知道何时应该释放它。 In what circumstance would you attempt to release it multiple times?在什么情况下你会尝试多次释放它?

Since lock.acquire() returns true if it successfully acquires the lock, you can store the state of locking in a local variable and then encapsulate lock.acquire() and the subsequent code inside a try-finally block.由于 lock.acquire() 如果成功获取锁则返回 true,因此您可以将锁定状态存储在局部变量中,然后将 lock.acquire() 和后续代码封装在 try-finally 块中。 Then in the finally block, you can query the variable to see if the lock has been acquired or not.然后在 finally 块中,您可以查询变量以查看是否已获取锁。 If it has, release.如果有,释放。

Create a quick wrapper function to check:创建一个快速包装函数来检查:

from multiprocessing import Lock

l = Lock()

def is_locked():
    locked = l.acquire(block=False)
    if locked == False:
        return True
    else:
        l.release()
        return False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 子类化 multiprocessing.Lock - Python subclassing multiprocessing.Lock 如何检查 multiprocessing.Lock() 是否被锁定? - How to check if a multiprocessing.Lock() is locked? 为什么multiprocessing.Lock()没有锁定Python中的共享资源? - Why does multiprocessing.Lock() not lock shared resource in Python? 在 mypy 中使用 python multiprocessing.Lock 作为参数类型 - Using python multiprocessing.Lock as an argument type in mypy 我将如何为字典中的每个元素创建一个 multiprocessing.Lock() ? - How would I create a multiprocessing.Lock() for each element in a dictionary? 尽管将 Python multiprocessing.Lock 作为目标函数参数传递,但在并行化时为无 - Python multiprocessing.Lock is Noned upon parallelizing despite passing it as target function argument 有没有理由使用threading.Lock over multiprocessing.Lock? - Is there any reason to use threading.Lock over multiprocessing.Lock? multiprocessing.Lock 是否在其上下文中暂停程序? - Does a multiprocessing.Lock pause the program within its context? 获取多处理。如果非阻塞或超时,则在 with 语句中锁定 - Acquire a multiprocessing.Lock in a with statement if non-blocking or with timeout 什么是multiprocessing.Lock的并发。未来等效? - What is the concurrent.futures-equivalent of multiprocessing.Lock?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM