Closed slhck closed 1 year ago
Hi, could you, please, provide more info, in particular your OS and fasteners version? I cannot reproduce your issue, and if I run two instances of the program you provided I get the expected exclusive behavior. In particular, I modified your example like this and ran in two terminals:
import time
import fasteners
lock=fasteners.InterProcessLock('.lock')
with lock:
print('hi')
time.sleep(10)
Oh, I get it now, the difference is that it's blocking by default! In your example it works — it does lock. I will close this. Thanks for your quick response!
Still: is there any way to get the context manager behavior working with a lock that does not block but raises an exception immediately?
No, context managers are not flexible and always block.
I thought about this at some point (context managers raising timeout exceptions), but don't remember now what was the conclusion :roll_eyes:
No problem, I can live with the example I gave above.
Maybe there is something that I don't understand about locks, but this code allows me to run two instances of the same program concurrently:
Whereas if I really want exclusive access, I have to implement something like:
In other words, what is the purpose of the context manager function if it does not actually enforce the lock?