ionelmc / python-redis-lock

Lock context manager implemented via redis SET NX EX and BLPOP.
https://pypi.python.org/pypi/python-redis-lock
BSD 2-Clause "Simplified" License
551 stars 77 forks source link

Idea: support N lock holders #42

Open sc0rp10 opened 8 years ago

sc0rp10 commented 8 years ago

Such as Consul offers:

An example use case is for highly-available N+1 deployments. In these cases, if N instances of a service are required, N+1 are deployed and use consul lock with -n=N to ensure only N instances are running. For singleton services, a hot standby waits until the current leader fails to take over.

AndreiPashkin commented 8 years ago

Sounds like a semaphore.

sc0rp10 commented 8 years ago

yes, sure, it's semaphore inside, but would be nice have two lock realizatons with same interface.

ionelmc commented 8 years ago

I would rather keep this library focused on its initial goal: a fast and robust lock for redis.

I don't have anything against implementing a semaphore but it seems to me that it would be better implemented in another library. Plus it's confusing, why would lock library implement a semaphore?

sc0rp10 commented 8 years ago

My little case: I need library, that can use Locks if holder=1 and use semaphore if holder=2-N: i don't like write code such as:

if holder == 1:
    with redis_lock.Lock(r, key):
        do_cricical_section()
else:
    with redis_lock.Semaphore(r, key, holder):
        do_cricical_section()

I prefer other approach:

with redis_lock.CommonLock(r, key, holder): # inside if holders > 1 using semaphore, otherwise - simple lock
    do_cricical_section()

Your library is the best among its competitors, would be nice if inside her will be implemented other synchronization primitives.