This example
|
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.") |
has an unreachable code path and implies an impossible use case.
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.
This example
python-redis-lock/README.rst
Lines 103 to 114 in 1497012
The example implies that a lock with an explicit ID can return
Falsefrom.acquire(blocking=False)AND have the lock owned by self perlock.get_owner_id() == host_idcheck, which is actually just the same check as_heldproperty, 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 returnFalseinstead of raisingAlreadyAcquired. I thought that was possible from reading the examples.Then when I got
AlreadyAcquiredI had to change my code totry .acquire(): except AlreadyAcquiredinstead ofif .acquire(), which is fine, except that the example further confused me by implying I might need to support bothFalseorAlreadyAcquiredin the event that I am unable to acquire the lock.