Redis distributed lock implementation for Python based on Pub/Sub messaging
Project description
Redis Lock with PubSub
Redis distributed lock implementation for Python based on Pub/Sub messaging.
1. Features
- Ensure atomicity by using the SETNX operation.
- Implements a Pub/Sub messaging system between the client attempting to acquire the lock and the one currently holding it.
- Includes a forced timeout mechanism to prevent infinite loops when attempting to acquire the lock.
- Supports asynchronous operations.
2. Installation
$> pip install redis-lock-py
Dependencies
- Python >= 3.9
- redis-py >= 5.2.0
3. Usage
3.1 Basic Example
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()
The redis-py library is required for Redis connection objects.
After successfully acquiring the lock using RedisLock.acquire
, ensure to release it by calling RedisLock.release
to prevent lock retention.
3.2 Using Context Managers
import redis
from redis_lock import RedisLock
client = redis.Redis(host="127.0.0.1", port=6379)
with RedisLock(client, name="foo", blocking_timeout=10):
print("Acquired lock successfully!")
To avoid issues where the lock remains unreleased (potentially blocking other clients from acquiring it),
you can use RedisLock
with a context manager, which ensures that the lock is automatically released at the end of the with
block.
Both examples in sections 3.1 and 3.2 function in a same manner.
3.3 With Asyncio
from redis.asyncio import Redis
from redis_lock.asyncio import RedisLock
client = Redis(host="127.0.0.1", port=6379)
async with RedisLock(client, name="foo", blocking_timeout=10):
print("Acquired lock successfully!")
redis-lock supports the asyncio platform.
3.4 Using Spin Lock
import redis
from redis_lock import RedisSpinLock
client = redis.Redis(host="127.0.0.1", port=6379)
lock = RedisSpinLock(client, name="foo")
if not lock.acquire(blocking=True, sleep_time=0.1):
raise Exception("Fail to acquire lock")
print("Acquired lock successfully!")
lock.release()
While a spin lock is available, it is not recommended unless there is a compelling reason to use it, as it is less efficient compared to the Pub/Sub messaging system.
System Flow
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file redis_lock_py-1.1.1.tar.gz
.
File metadata
- Download URL: redis_lock_py-1.1.1.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8de4804d09ea0355a6e4157a7aedccc5c9cdcc02d0830d830b3072601f74c010 |
|
MD5 | d123f33bdde922d9323f7721330e8a65 |
|
BLAKE2b-256 | 17b3895706e0318d9ae8a4aa43fcb36047845a7377796e7396611343156b62bc |
File details
Details for the file redis_lock_py-1.1.1-py3-none-any.whl
.
File metadata
- Download URL: redis_lock_py-1.1.1-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24039e42bd1156d22552ecd94662b965fe54ba97614f4675e8976b7512fc3555 |
|
MD5 | 29fb801eb3b205d71b16f1a11c292c24 |
|
BLAKE2b-256 | 10fc28a50686926352548d9e841ccfdcc5408dbf09fce49d54fcb6068deba21b |