Skip to main content

A cluster-supported distributed lock on Redis and Python

Project description

Redistock

A simple and easy-to-use and cluster-supported distributed lock implement based on Redis and Python.

Installation

pip install redistock

Usage

Simple usage:

import redis
from redistock import Redistock

redis_conn = redis.StrictRedis(...)

# simple
lock = Redistock(redis_conn, 'name')
if lock.acquire():
    print('Obtained lock')
lock.release()

# with statement
with Redistock(redis_conn, 'name'):
    print('Obtained lock')

Advanced usage:

import redis
from redistock import Redistock
from redistock import RedistockNotObtained

redis_conn = redis.StrictRedis(...)


with Redistock(redis_conn, 'name', ttl=1):
    # will be blocked for 1 seconds
    print('Obtained lock')


with Redistock(redis_conn, 'name', ttl=0.001):
    # will be blocked for 1 milliseconds
    print('Obtained lock')


with Redistock(redis_conn, 'name', delay=1):
    # will delay 1 seconds between twice retry, default 0.001
    print('Obtained lock')


lock = Redistock(redis_conn, 'name', block=False)
# do not retry, raise RedistockNotObtained if not obtained lock
if lock.acquire():
    print('Obtained lock')
lock.release()


try:
    with Redistock(redis_conn, 'name', timeout=10):
        # will try to acquire for 10 seconds, success or raise RedistockNotObtained
        # after 10 seconds
        print('Obtained lock')
except RedistockNotObtained:
    print('Not obtained lock')

Download files

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

Files for redistock, version 0.1.3
Filename, size File type Python version Upload date Hashes
Filename, size redistock-0.1.3-py2.py3-none-any.whl (4.0 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes
Filename, size redistock-0.1.3.tar.gz (3.4 kB) File type Source Python version None Upload date Hashes View hashes

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 DigiCert DigiCert EV certificate StatusPage StatusPage Status page