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.10
- redis-py >= 7.0.1
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
Metadata
Release files for redis-lock-py 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| redis_lock_py-1.3.0.tar.gz | 7.8 kB | Without provenance | Details for redis_lock_py-1.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| redis_lock_py-1.3.0-py3-none-any.whl | Python 3 | none | any | Details for redis_lock_py-1.3.0-py3-none-any.whl |
Total release size: 17.2 kB
Release files / redis_lock_py-1.3.0.tar.gz
| Download URL | redis_lock_py-1.3.0.tar.gz |
|---|---|
| Size | 7.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8ab9a65e4f06c9c5be44ad6e5d9065a1685ac264c421ac4c8687f49d0b82166a
|
|
BLAKE2b-256 checksum How to use checksums |
05381d126ffb978b1b94fa22b1105276b94e3faeee752c714a2c207654f1bfa8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / redis_lock_py-1.3.0-py3-none-any.whl
| Download URL | redis_lock_py-1.3.0-py3-none-any.whl |
|---|---|
| Size | 9.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ab7268a06e13cbc3ad1e0290afda1d0427185a363552e53de57cecdc9efe1fe8
|
|
BLAKE2b-256 checksum How to use checksums |
d45aebeb4fe6fd57a15ddb3f6ba3e1b214691d1e44a2c1e0be5740632782a0a8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for redis-lock-py 1.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.