Registry / database / python-redis-lock

python-redis-lock

JSON →
library4.0.1pypypi✓ verified 24d ago

python-redis-lock is a Python library that provides a distributed lock context manager, implemented using Redis's `SETNX` (SET if Not eXists) and `BLPOP` operations. It aims to offer an interface similar to Python's built-in `threading.Lock`. The current version is 4.0.1, and it maintains an active release cadence, with its latest major update in late 2022.

pip install python-redis-lock
INSTALL
IMPORT
SIG · PYTHON-REDIS-LOCK
P
python-redis-lock
databasepythonv4.0.1
Install
2.0s avg
Import
454ms
Disk
21MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.488s · 23MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.0s · import 0.420s · 24MB
21MB installed
● package 21MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Lock
from redis_lock import Lock
from redis.lock import Lock
The `redis` (redis-py) library also has a `Lock` class under `redis.lock`. Ensure you import from `redis_lock` for this specific library's implementation.

This quickstart demonstrates how to acquire and use a distributed lock with `python-redis-lock` using a context manager. It highlights setting an expiration (`expire`) and enabling automatic renewal (`auto_renewal`) to prevent locks from being held indefinitely if the application crashes. It also shows checking the lock status.

import redis import time import os from redis_lock import Lock REDIS_HOST = os.environ.get('REDIS_HOST', 'localhost') REDIS_PORT = int(os.environ.get('REDIS_PORT', 6379)) # Connect to Redis client = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, db=0) lock_name = "my-distributed-lock" # Acquire a lock using a context manager, with a 10-second expiration # and automatic renewal while the 'with' block is active. print(f"Attempting to acquire lock '{lock_name}'...") with Lock(client, lock_name, expire=10, auto_renewal=True, blocking_timeout=5) as lock: if lock.acquired: print(f"Lock '{lock_name}' acquired! Doing some critical work...") # Simulate work that takes longer than the expire time # auto_renewal will keep the lock alive. time.sleep(15) print(f"Work finished. Lock '{lock_name}' will be automatically released.") else: print(f"Failed to acquire lock '{lock_name}'. Another process holds it.") # You can also check if a lock is currently held (by anyone) if Lock(client, lock_name).locked(): print(f"Lock '{lock_name}' is currently held by someone else (or was not properly released).") else: print(f"Lock '{lock_name}' is not currently held.") client.close()
Debug
Known issues
breakingVersion 4.0.0 dropped support for Python 2.7 and Python 3.6. Users on these older Python versions must use `python-redis-lock < 4.0.0`.
fix
Upgrade to Python 3.7+ or pin `python-redis-lock` to a version prior to 4.0.0.
affects: >=4.0.0
gotchaIf an application crashes while holding a lock and `expire` (timeout) is not set, the lock may remain in Redis indefinitely, causing permanent deadlocks. Setting `expire` alone will release the lock after a fixed duration, but if the critical section takes longer, the lock might be released prematurely.
fix
Always use the `auto_renewal=True` parameter in conjunction with `expire` when creating a `Lock` instance. This ensures the lock is automatically renewed as long as the Python process is running within the `with` block, and will expire cleanly if the process terminates unexpectedly. Example: `Lock(client, 'my-lock', expire=60, auto_renewal=True)`.
affects: <4.0.1
deprecatedThe `force` parameter on the `release()` method was removed in version 3.0.0. This option was considered to encourage sloppy programming and is no longer available.
fix
Review your code and remove any usage of `lock.release(force=True)`. Ensure proper lock acquisition and release logic without relying on force-releasing locks, which can lead to race conditions.
affects: >=3.0.0
gotcha`python-redis-lock` implements a distributed lock based on Redis's `SETNX` and `BLPOP`. It is *not* an implementation of the more complex Redlock algorithm (which involves multiple independent Redis instances for higher fault tolerance).
fix
Understand that this library is suitable for single-instance Redis distributed locking. If your application requires the guarantees of the Redlock algorithm (e.g., across multiple Redis masters), you will need to use a different library (e.g., `redlock-py`) or implement Redlock logic yourself. Do not confuse `redis_lock.Lock` with `redis.lock.Lock` (from `redis-py`), as they are separate implementations.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'redis_lock'
The `python-redis-lock` library is not installed in the current Python environment, or the `redis_lock` module cannot be found.
fix
Install the library using pip: `pip install python-redis-lock`
redis.exceptions.ConnectionError: Error 111 connecting to 127.0.0.1:6379. Connection refused.
The Python application cannot establish a connection to the Redis server, usually because the server is not running, is inaccessible, or the connection details (host, port) are incorrect.
fix
Ensure your Redis server is running and accessible. Verify the host and port in your Redis client instantiation, e.g., `client = redis.StrictRedis(host='localhost', port=6379)`.
redis_lock.LockError: Could not acquire lock 'my-lock' on redis_client
The lock could not be acquired within the specified timeout duration, meaning another process or thread currently holds the lock, or the timeout period was too short.
fix
Increase the `timeout` parameter when creating the `Lock` object or when calling `acquire()`, or investigate the process holding the lock to ensure it's released promptly. Example: `lock = redis_lock.Lock(client, 'my-lock', expire=10, timeout=5)`.
TypeError: acquire() got an unexpected keyword argument 'blocking'
The `acquire()` method of `redis_lock.Lock` does not accept a `blocking` argument; it expects a `timeout` argument instead. This often happens when migrating from `threading.Lock` or similar interfaces.
fix
Replace the `blocking` argument with `timeout`. For a non-blocking attempt, use `lock.acquire(timeout=0)`. For a blocking attempt, either omit `timeout` or set it to a desired wait duration. Example: `lock.acquire(timeout=5)`.
Upgrade
Version history
4.0.1latest on PyPI · released Apr 8, 2026
Audit
Dependencies
redisrequiredRequired for Redis client interaction, as the library builds on top of `redis-py`.
Agent activity
8 hits · last 30 days
node
6
Resources
python-redis-lock — pip install python-redis-lock · libregistry