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 SETNX operation
  • Pub/Sub messaging system between the client waiting to get the lock and holding the lock
  • Force timeout to avoid infinite loops when trying to acquire lock
  • Async is supported

2. Installation

$> pip install redis-lock-py

Dependencies

  • Python >= 3.7
  • redis-py >= 4.2.0

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

redis-py library is required for redis connection objects. The RedisLock.release method must be invoked to release the lock after acquiring a lock successfully by calling RedisLock.acquire method with returned True.

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, "foo", blocking_timeout=10):
    print("Acquired lock successfully!")

If the part that releases the lock is missing after acquire a lock, all the clients that access the same name may not be able to acquire the lock. To prevent this unexpected malfunction from happening, programmed to unlock the lock by itself at the end of the with context. Both examples in 3.1 and 3.2 work the same way.

3.3 Using Spin Lock

import redis
from redis_lock import RedisSpinLock

client = redis.Redis(host="127.0.0.1", port=6379)

lock = RedisSpinLock(client, "foo")
if not lock.acquire(blocking=True, sleep_time=0.1):
    raise Exception("Fail to acquire lock")
print("Acquired lock successfully!")
lock.release()

Spin lock is also available, but not recommended unless there is a compelling reason to use them because of inefficiency compare to the Pub/Sub messaging system.

3.4 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, "foo", blocking_timeout=10):
    print("Acquired lock successfully!")

redis-lock supports asyncio platform.

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.0.0.tar.gz (6.8 kB view hashes)

Uploaded Source

Built Distribution

redis_lock_py-1.0.0-py3-none-any.whl (8.7 kB view hashes)

Uploaded Python 3

Supported by

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