--- id: python-redis-lock version: "4.0.1" license: BSD-2-Clause license_treatment: permissive maintenance: active --- # python-redis-lock — Lock context manager implemented via redis SETNX/BLPOP. License: permissive · Maintenance: active · Downloads: 1.2M/mo ## 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 above — 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 pip install python-redis-lock uv add python-redis-lock poetry add python-redis-lock ## Installing 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_current - Install friction: low - Maintenance: active - Downloads: 1.2M/month (top 5,000 on PyPI) - Known vulnerabilities: none known ## Tags redis distributed lock, cross-process locking, redis lock manager, distributed mutual exclusion, redis setnx lock, blocking redis lock, lock with expiry, distributed-locking, redis, concurrency [View on SkillFed](https://skillfed.io/packages/python-redis-lock) · [View on PyPI](https://pypi.org/project/python-redis-lock/)