Coder Social home page Coder Social logo

redistock's Introduction

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

redistock's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.