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.0.1.tar.gz (5.1 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for python-redis-lock-0.0.1.tar.gz
Algorithm Hash digest
SHA256 29dfb74efc78aed46dc7df29a3c0621f03f8ac793033a336b9622446f6823c14
MD5 a21cc2b48ef931670470fda7db471eda
BLAKE2b-256 805f3a6e66ad7af458e531bf7243518af2dc1a006eebc8c38a8ce3eebf6911dc

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