Skip to main content

Distributed lock that works on top of the Redis cluster or single node.

Project description

Redis cluster distributed lock

Distributed lock that works on top of the Redis cluster or single node

Installation

pip install redis_cluster_lock

Usage

Simple usage with single redis node:

from redis_cluster_lock import SimpleLock
from redis import StrictRedis

redis_client = StrictRedis(...)

with SimpleLock(redis_client, 'lock:name'):
    print('Locked code here')

Simple usage with redis cluster:

from redis_cluster_lock import SimpleLock
from rediscluster import StrictRedisCluster

redis_client = StrictRedisCluster(...)

with SimpleLock(redis_client, 'lock:name'):
    print('Locked code here')

Advanced usage:

from redis_cluster_lock import SimpleLock
from redis_cluster_lock.error import NotAcquired
from redis import StrictRedis

redis_client = StrictRedis(...)

lock = SimpleLock(
    redis_client=redis_client,
    key='lock:name',
    blocking=True,
    timeout=60,
    # lock will be auto-renewed while SimpleLock object is alive
    # use any positive integer to set expiring lock
    expire=0
)

# will be blocked for 60 seconds
if not lock.acquire():
    print('Can not acquire lock')
    exit()

print('Locked code here')

try:
    # use strict=False to avoid rising exception
    lock.release(strict=True)
except NotAcquired:
    print('Lock was not acquired')

Project details


Release history Release notifications

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
redis_cluster_lock-0.1.1-py2.py3-none-any.whl (4.9 kB) Copy SHA256 hash SHA256 Wheel py2.py3
redis-cluster-lock-0.1.1.tar.gz (4.6 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page