Skip to main content

description

This is yet another distributed lock for Python with interface compatible with standard Lock/RLock class (only constructor parameters are different and release method has one optional parameter force)

Currently there is only one implementation, based on Redis, but it’s very easy to extend base class and adapt it to any other distributed storage (like Etcd, databases - both relational and NoSQL, distributed file systems etc.)

Redis lock

usage

examples:

Create lock object

from PyYADL import RedisLock
lock = RedisLock(name='test_lock', prefix='my_app', ttl=60, existing_connection_pool=None, redis_host='127.0.0.1', redis_port=6379, redis_password='secret', redis_db=0)

Parameters meaning: * name - each resource should have unique lock name, which would be shared across all systems. Required * prefix - prefix useful to avoid conflicts in names. Optional * ttl - how many seconds lock will be active. If ttl <= 0, lock will be valid until release. Optional Default: -1 * existing_connection_pool already established connection pool Optional * redis_host Optional Default: localhost * redis_port Optional Default: 6379 * redis_password Optional * redis_db Optional Default: 0

Basic usage

from PyYADL import RedisLock

lock = RedisLock('test_lock')
lock.acquire()
lock.release()

Basic lock and release operations. If lock already acquired, will wait for release or ttl expire

from PyYADL import RedisLock

lock = RedisLock('test_lock')
with lock:
    # do some tasks
    pass

Lock and release using context manager

from PyYADL import RedisLock

lock1 = RedisLock('test_lock')
lock2 = RedisLock('test_lock')
lock1.acquire()
lock2.release()

Will raise RuntimeError (because lock is owned by other instance)

from PyYADL import RedisLock

lock1 = RedisLock('test_lock')
lock2 = RedisLock('test_lock')
lock1.acquire()
lock2.release(force=True)

Will release lock, because force parameter is set to True

from PyYADL import RedisLock

lock = RedisLock('test_lock')
status = lock.acquire(blocking=False)

Will acquire lock and return True, if lock released, otherwise return False without waiting

from PyYADL import RedisLock

lock = RedisLock('test_lock')
status = lock.acquire(timeout=12)

Will try to acquire lock for 12 seconds. In case of success will return True, otherwise return False

Read and Write locks

There are two lock subtypes: * Write Lock (typical lock, exclusive) * Read Lock (non exclusive)

At the same time, there can be only one write lock (mainly for changes) or many read lock (mainly for read operations). If write lock has been acquired, read lock cannot be obtained and when at least one read lock exists, write lock cannot be acuired.

Usage

Examples

from PyYADL import RedisWriteLock

lock = RedisWriteLock('test_lock')
status = lock.acquire(blocking=True, timeout=20)

Equivalent of RedisLock class

from PyYADL import RedisReadLock

lock1 = RedisReadLock('test_lock')
lock2 = RedisReadLock('test_lock')
lock1.acquire()
lock2.acquire()

Will create two read locks (at the same time there can be many read locks)

from PyYADL import RedisReadLock, RedisWriteLock

lock1 = RedisReadLock('test_lock')
lock2 = RedisReadLock('test_lock')
lock3 = RedisWriteLock('test_lock')
lock1.acquire()
lock2.acquire()
lock3.acquire()

Will acquire only lock1 and lock2 (write lock can’t be obtained when other locks exists)

from PyYADL import RedisReadLock, RedisWriteLock

lock1 = RedisWriteLock('test_lock')
lock2 = RedisReadLock('test_lock')

lock1.acquire()
lock2.acquire()

Will acquire only lock1 (when write lock exists, read lock cannot be obtained)

Release files for PyYADL 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for PyYADL 1.0.0
File Interpreter ABI Platform
PyYADL-1.0.0-py3-none-any.whl Python 3 none any Details

Release files / PyYADL-1.0.0-py3-none-any.whl

Download URL PyYADL-1.0.0-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0a245151874e20da60fd35211c98d79ffbb714474fa0f0845cbb7174d0bce662
BLAKE2b-256 checksum
How to use checksums
adbb7d2db297ef34a98f719caf70eb60603e8cde51d5a00f28ea2f0e9eb01074
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0.0 This release

1 release file

0.1.1

2 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page