Skip to main content

Redis distributed lock implementation for Python based on Pub/Sub messaging

Project description

License PyPI Release Downloads Python Support Implementation codecov

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

redis-lock-flow

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

redis_lock_py-1.2.1.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

redis_lock_py-1.2.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file redis_lock_py-1.2.1.tar.gz.

File metadata

  • Download URL: redis_lock_py-1.2.1.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for redis_lock_py-1.2.1.tar.gz
Algorithm Hash digest
SHA256 b7611c2ded32d28c86301e8099b6e6512f7e264c3ead469276b220d10a07614f
MD5 aa324fced3d2e45895068863bc1aa4be
BLAKE2b-256 61e1a334cbd7d8a18a4b4948be7bb1cc484a58288893a04a24e2c92091eb2816

See more details on using hashes here.

File details

Details for the file redis_lock_py-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: redis_lock_py-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for redis_lock_py-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 96d581869b6dadc78d23a668924aab9702329c507d24aea18eadc53cfecce344
MD5 efba86f7f29d1fd7e02d1ac004b30644
BLAKE2b-256 9090a0b21c33e8ae4f38e55c53449c9a7fe5b695565d190a633b4a1b6c18f8e1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page