Skip to content

Why can I acquire the same lock twice? #262

Answered by asqui
fostiropoulos asked this question in Q&A
Discussion options

You must be logged in to vote

The reason that you can acquire the lock multiple times (within the same thread) like this is because FileLocks are reentrant, meaning that if you already hold the lock in a given thread, you can acquire it a second time in the same thread.

If you execute this code snippet in a second thread, or process (e.g. just run a second instance of your code snippet in a separate python interpreter) you'll see that the lock.acquire() will block.

See the last example at https://py-filelock.readthedocs.io/en/latest/#tutorial which illustrates this behaviour, and the Python standard library documentation for RLock for an explanation of reentrant locks: https://docs.python.org/3/library/threading.html#…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by gaborbernat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #261 on August 18, 2023 14:38.