-
Notifications
You must be signed in to change notification settings - Fork 83
Description
This example
Lines 103 to 114 in 1497012
| import socket | |
| host_id = "owned-by-%s" % socket.gethostname() | |
| lock = redis_lock.Lock(conn, "name-of-the-lock", id=host_id) | |
| if lock.acquire(blocking=False): | |
| assert lock.locked() is True | |
| print("Got the lock.") | |
| lock.release() | |
| else: | |
| if lock.get_owner_id() == host_id: | |
| print("I already acquired this in another process.") | |
| else: | |
| print("The lock is held on another machine.") |
The example implies that a lock with an explicit ID can return False from .acquire(blocking=False) AND have the lock owned by self per lock.get_owner_id() == host_id check, which is actually just the same check as _held property, which will instead cause .acquire(blocking=False) to raise an exception.
Ideally I would like to be able to give locks in multiple processes the same ID and be able to call .acquire(blocking=False) and have it return False instead of raising AlreadyAcquired. I thought that was possible from reading the examples.
Then when I got AlreadyAcquired I had to change my code to try .acquire(): except AlreadyAcquired instead of if .acquire(), which is fine, except that the example further confused me by implying I might need to support both False or AlreadyAcquired in the event that I am unable to acquire the lock.