Lock context manager implemented via redis SETNX/BLPOP.
Project description
Lock context manager implemented via redis SETNX/BLPOP.
Interface targeted to be exactly like threading.Lock.
Usage
Because we don’t want to require users to share the lock instance across processes you will have to give them names. Eg:
conn = StrictRedis() with redis_lock.Lock(conn, "name-of-the-lock"): print("Got the lock. Doing some work ...") time.sleep(5)
Eg:
lock = redis_lock.Lock(conn, "name-of-the-lock") if lock.acquire(blocking=False): print("Got the lock.") else: print("Someone else has the lock.")
Features
based on the standard SETNX recipe
optional expiry
no spinloops at acquire
Implementation
redis_lock will use 2 keys for each lock named <name>:
lock:<name> - a string value for the actual lock
lock-signal:<name> - a list value for signaling the waiters when the lock is released
This is how it works:
TODO
???
Requirements
Redis 2.6 or later.
Python 2.6, 2.7, 3.2, 3.3 and PyPy are supported.
Similar projects
bbangert/retools - acquire does spinloop
distributing-locking-python-and-redis - acquire does polling
cezarsa/redis_lock - acquire does not block
andymccurdy/redis-py - acquire does spinloop
mpessas/python-redis-lock - blocks fine but no expiration
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.