skillfed

python-redis-lock

Lock context manager implemented via redis SETNX/BLPOP.

python-redis-lock v4.0.1 1.2M downloads/30d#4,294 on PyPI570
Permissive license BSD-2-Clause Active released

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-lock

uv

uv add python-redis-lock

poetry

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 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

Development Status :: 5 - Production/StableIntended Audience :: DevelopersOperating System :: Microsoft :: WindowsOperating System :: POSIXOperating System :: UnixProgramming Language :: PythonProgramming Language :: Python :: 3Programming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Programming Language :: Python :: Implementation :: CPythonProgramming Language :: Python :: Implementation :: PyPyTopic :: Utilities

Tags

redis distributed lockcross-process lockingredis lock managerdistributed mutual exclusionredis setnx lockblocking redis locklock with expiry
distributed-lockingredisconcurrency

More Utilities packages