Skip to main content

Lock context manager implemented via redis SETNX/BLPOP.

Project description

Build Status Coverage Status PYPI Package PYPI Package

Lock context manager implemented via redis SETNX/BLPOP.

Interface targeted to be exactly like threading.Lock.

Usage

Because we don’t want to require users to share the lock instance across processes you will have to give them names. Eg:

conn = StrictRedis()
with redis_lock.Lock(conn, "name-of-the-lock"):
    print("Got the lock. Doing some work ...")
    time.sleep(5)

Eg:

lock = redis_lock.Lock(conn, "name-of-the-lock")
if lock.acquire(blocking=False):
    print("Got the lock.")
else:
    print("Someone else has the lock.")

Features

  • based on the standard SETNX recipe

  • optional expiry

  • no spinloops at acquire

Implementation

redis_lock will use 2 keys for each lock named <name>:

  • lock:<name> - a string value for the actual lock

  • lock-signal:<name> - a list value for signaling the waiters when the lock is released

This is how it works:

python-redis-lock flow diagram

TODO

  • ???

Requirements

Redis 2.6 or later.

Python 2.6, 2.7, 3.2, 3.3 and PyPy are supported.

Similar projects

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

python-redis-lock-0.1.0.tar.gz (40.1 kB view details)

Uploaded Source

File details

Details for the file python-redis-lock-0.1.0.tar.gz.

File metadata

File hashes

Hashes for python-redis-lock-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4f047915f11cb2b2283e7a5c93c649e6c48a02841d15899f0dbf952f7cb8b399
MD5 fe6a035ceef95a19abe6dbba41bfb37d
BLAKE2b-256 61cd1ba727fe1f0092c20a31670fd17c36a8261778b060f6bcd39dcd21a24a09

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