Skip to content

Documentation bug in usage example #120

@mrmachine

Description

@mrmachine

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions