Skip to main content

A library for python distributed lock, optimistic lock and limiter

Project description

Pie-lock

All lock module using redis for control.

Package version Supported Python versions

Installation

With Pypi:

pip install pie-lock

With Github:

pip install git+https://github.com/bacsiTuan/pielock.git

Usage Distributed Lock

key = "test1"
success, msg = redis_lock.acquire(key)
print(msg)
if not success:
    print(msg)
redis_lock.release(key)

Usage Optimistic Lock

def test_optimistic_lock(self):
    is_locked1, msg = redis_lock.acquire("key1")
    if not is_locked1:
        print(msg)
    is_locked2, msg = redis_lock.acquire("key1")
    if not is_locked2:
        print(msg)
    is_locked3, msg = redis_lock.acquire("key1")
    if not is_locked3:
        print(msg)
    release, msg = redis_lock.release("key1")
    if not release:
        print(msg)
    is_locked4, msg = redis_lock.acquire("key1")
    if not is_locked4:
        print(msg)

Configuration

Redis configuration

from pie_lock.backends import DistributedLock

redis_lock = DistributedLock(
    expires=5,
    timeout=5,
    retry_after=1, # seconds between retries
    tries=32,  # max number of tries
)
redis_lock.get_client(
    host="localhost",
    port=19821,
    password="passsword",
    username="default"
)

Note: all fields after the scheme are optional, and will default to localhost on port 6379, using database 0.

DEFAULT_TIMEOUT (default: 60)

If another client has already obtained the lock, sleep for a maximum of this many seconds before giving up. A value of 0 means no wait (give up right away).

The default timeout can be overridden when instantiating the lock.

Limiter

Based on sliding window algorithm

from pie_lock.backends import Limiter, TimeUnit

redis = Limiter(
    host="localhost",
    port=19821,
    password="passsword",
    username="default",
    socket_timeout=2,
)

for i in range(6):
    allow, msg = redis.allow(redis_key="mylist", per=TimeUnit.SECOND, count=2)
    if not allow:
        print(msg)
time.sleep(1)
allow, msg = redis.allow(redis_key="mylist", per=TimeUnit.SECOND, count=2)
if not allow:
    print(msg)

We consider any existing lock older than this many seconds to be invalid
in order to detect crashed clients. This value must be higher than it
takes the critical section to execute.

The default expires can be overridden when instantiating the lock.

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

pie_lock-0.1.7.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pie_lock-0.1.7-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file pie_lock-0.1.7.tar.gz.

File metadata

  • Download URL: pie_lock-0.1.7.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.9.13 Darwin/22.1.0

File hashes

Hashes for pie_lock-0.1.7.tar.gz
Algorithm Hash digest
SHA256 cb4b13570d7bb6b2d9cb61b930ec9c6040f6e68d85a3c5edf25f86795b50d582
MD5 0a070a914e8f48159f20cc395b76dbd5
BLAKE2b-256 4e28ab26c1a552ac68f65564087e2bb758e29bfc70a7903612bb7b94c9ec07ae

See more details on using hashes here.

File details

Details for the file pie_lock-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: pie_lock-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.9.13 Darwin/22.1.0

File hashes

Hashes for pie_lock-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 91207bfb4695563b9710d54cf681b34248e6574587d36660fce92a070cf44b9d
MD5 46766d18aa6c772df89e04a406f7a5b4
BLAKE2b-256 8f0a2a4ef1517bbd2cc1fcdcff30c3db91a4d9c7258aca87f6a67343b6fd7266

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