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

2024/12/9 1:37:44
>>> 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. How can I prevent to release a lock more than once? Something like l.is_released() ?

Answer

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's Lock once unlocks releases ALL other threads that acquired() on the same lock and blocked for the time being. 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.

(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)

https://en.xdnf.cn/q/73397.html

Related Q&A

How do you ensure a Celery chord callback gets called with failed subtasks?

I am using a Chord in Celery to have a callback that gets called when a Group of parallel tasks finish executing. Specifically, I have a group of functions that wrap calls to an external API. I want to…

Unpacking nested C structs in Python

I am trying to unpack a C struct that is handed to my Python program in binary form and includes another nested struct. The relevant part of the C header looks like this:typedef struct {uint8_t seq;uin…

Remove black borders on images with watermarks in Python

I have a bunch of image I would like to uniformise by removing black borders. Usually I use the Trim function of Imagemagick with the fuzz parameters but in the case the image have some watermark the r…

scipy cdist with sparse matrices

I need to calculate the distances between two sets of vectors, source_matrix and target_matrix.I have the following line, when both source_matrix and target_matrix are of type scipy.sparse.csr.csr_matr…

NumPy arrays with SQLite

The most common SQLite interface Ive seen in Python is sqlite3, but is there anything that works well with NumPy arrays or recarrays? By that I mean one that recognizes data types and does not requir…

Binary Phase Shift Keying in Python

Im currently working on some code to transmit messages/files/and other data over lasers using audio transformation. My current code uses the hexlify function from the binascii module in python to conve…

Django. Listing files from a static folder

One seemingly basic thing that Im having trouble with is rendering a simple list of static files (say the contents of a single repository directory on my server) as a list of links. Whether this is sec…

inconsistent migration history when changing a django apps name

Im trying to rename one of the apps in my django website. There is another app which depends on it and its mysql tables. I went over all the files in both apps and changed the instances of the old name…

Tensorflow vs Numpy math functions

Is there any real difference between the math functions performed by numpy and tensorflow. For example, exponential function, or the max function? The only difference I noticed is that tensorflow take…

PyQt5: I cant understand QGraphicsScenes setSceneRect(x, y, w, h)

I see some people say if you want to put QGraphicsScenes origin of coordinates at the origin of QGraphicsView, i.e. top-left corner. You need to let both of them have the same size.So here is what I do…