python-redis-lock
Lock context manager implemented via redis SETNX/BLPOP.
What it is and what it does
python-redis-lock is a distributed locking library that uses Redis to coordinate access to shared resources across multiple processes or machines. It implements a Lock class with an interface matching Python's threading.Lock, so it integrates naturally into existing code patterns. The lock uses Redis SETNX for acquisition and BLPOP for efficient waiting, avoiding spinloops and polling.
The package supports optional expiry (to recover from crashes), auto-renewal (to keep a lock active while your code runs), timeouts, and owner identification (to detect which process or machine holds a lock). It works with redis and includes a Django cache backend integration for preventing cache-stampede problems. Maintenance is active, Python 3.7 or later is supported, and there are no known security vulnerabilities.
Use it for:
- Prevent multiple application instances from running the same scheduled task simultaneously in a distributed system.
- Coordinate database migrations or schema changes across a cluster without conflicts.
- Implement cache-stampede prevention in Django by locking cache misses so only one request recomputes expensive data.
- Protect critical sections in microservices that share state via Redis.
- Recover from application crashes by setting an expiry on locks so they auto-release after a timeout.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Provides a Redis-backed distributed lock with a threading.Lock-like interface, supporting blocking/non-blocking acquire, expiry, auto-renewal, and owner identification across processes.
Yes. This is a mature, actively maintained library (last release 128 days ago) with a simple, well-understood interface, zero security issues, and low install friction. Use it when you need cross-process locking backed by Redis and want to avoid spinloops or polling. The only prerequisite is a running Redis server.
Install
python-redis-lock on PyPI
pip
pip install python-redis-lockuv
uv add python-redis-lockpoetry
poetry add python-redis-lockInstalling python-redis-lock
Before you install
Low friction: pure Python wheel with a single runtime dependency (redis). Actively maintained with recent release (128 days ago), 570 repository stars, and no known vulnerabilities.
License in practice
BSD-2-Clause is permissive; you may use, modify, and distribute this package freely in commercial and open-source projects with minimal restrictions.
Quickstart
from redis import Redis
import python_redis_lock
conn = Redis()
lock = python_redis_lock.Lock(conn, "my-lock")
if lock.acquire(blocking=False):
print("Got the lock")
lock.release()
# Or as context manager:
with python_redis_lock.Lock(conn, "my-lock"):
print("Lock held during this block")
Requires a running Redis server (2.6.12 or later) accessible via the redis connection object.
Verify before relying
- Whether auto_renewal thread overhead is acceptable for your workload and latency requirements.
- Performance characteristics under high contention or with many lock holders across processes.
- Exact behavior and guarantees of the SETNX/BLPOP implementation under network partitions.
Package facts
| License | BSD-2-Clause (permissive) |
| Python support | supports the current Python release (>=3.7) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — redis |
| Maintenance | actively maintained — 128 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 1,152,958/month — #4,294 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: python_redis_lock-4.0.1-py3-none-any.whl
Tags
More Utilities packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
charset-normalizerDetects and normalizes text encoding from…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
pluggyPluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
PygmentsPygments is a syntax highlighter that colorizes…
permissive · top 100 on PyPI
sixSix provides utility functions to write Python…
permissive · top 100 on PyPI
potteryPottery provides Python dict, set, list, deque,…
unclear · top 15,000 on PyPI
redlockImplements the RedLock algorithm to provide…
permissive · top 15,000 on PyPI
redlock-pyImplements Redis-based distributed locks using…
permissive · top 15,000 on PyPI
aioredlockImplements the Redlock distributed lock…
permissive · top 15,000 on PyPI
PyMySQLLockProvides distributed locking primitives backed…
permissive · top 15,000 on PyPI
python-dynamodb-lockProvides distributed locking on top of DynamoDB…
permissive · top 15,000 on PyPI
portalockerPortalocker provides cross-platform file…
permissive · top 1,000 on PyPI
aiorwlockProvides read-write locks for asyncio programs,…
permissive · top 5,000 on PyPI
celery_oncePrevents duplicate execution and queuing of…
permissive · top 15,000 on PyPI
celery-singletonPrevents duplicate Celery tasks from being…
permissive · top 15,000 on PyPI