Redis distributed lock implementation for Python based on Pub/Sub messaging.
- Ensure atomicity by using SETNX operation
- Pub/Sub messaging system between the client waiting to get the lock and holding the lock
- Force timeout to avoid infinite loops when trying to acquire lock
- Async is supported
$> pip install redis-lock-py
- Python >= 3.7
- redis-py >= 4.2.0
import redis
from redis_lock import RedisLock
client = redis.Redis(host="127.0.0.1", port=6379)
name = "foo"
lock = RedisLock(client, name)
if not lock.acquire():
raise Exception("Fail to acquire lock")
print("Acquired lock successfully!")
lock.release()
redis-py library is required for redis connection objects.
The RedisLock.release
method must be invoked to release the lock after acquiring a lock successfully by calling RedisLock.acquire
method with returned True
.
import redis
from redis_lock import RedisLock
client = redis.Redis(host="127.0.0.1", port=6379)
with RedisLock(client, "foo", blocking_timeout=10):
print("Acquired lock successfully!")
If the part that releases the lock is missing after acquire a lock,
all the clients that access the same name
may not be able to acquire the lock.
To prevent this unexpected malfunction from happening, programmed to unlock the lock by itself at the end of the with
context.
Both examples in 3.1 and 3.2 work the same way.
import redis
from redis_lock import RedisSpinLock
client = redis.Redis(host="127.0.0.1", port=6379)
lock = RedisSpinLock(client, "foo")
if not lock.acquire(blocking=True, sleep_time=0.1):
raise Exception("Fail to acquire lock")
print("Acquired lock successfully!")
lock.release()
Spin lock is also available, but not recommended unless there is a compelling reason to use them because of inefficiency compare to the Pub/Sub messaging system.
from redis.asyncio import Redis
from redis_lock.asyncio import RedisLock
client = Redis(host="127.0.0.1", port=6379)
async with RedisLock(client, "foo", blocking_timeout=10):
print("Acquired lock successfully!")
redis-lock supports asyncio platform.