Skip to main content

black

To protect tree-like resources:

https://raw.githubusercontent.com/SewanDevs/mutextree/master-github/images/mutextree-voc.png

Nodes have to be represented by their fully qualified name. For instance, the node D is represented by [A, B, D]:

https://raw.githubusercontent.com/SewanDevs/mutextree/master-github/images/mutextree-qualified-name.png

The algorithm to lock a node is the following:

  1. We lock the parent nodes in order:

https://raw.githubusercontent.com/SewanDevs/mutextree/master-github/images/mutextree-step1.png https://raw.githubusercontent.com/SewanDevs/mutextree/master-github/images/mutextree-step2.png
  1. Then we check that no child node is already locked:

https://raw.githubusercontent.com/SewanDevs/mutextree/master-github/images/mutextree-step3.png
  1. We lock the wanted node

https://raw.githubusercontent.com/SewanDevs/mutextree/master-github/images/mutextree-step4.png
  1. We release all the parent locks that are no longer usefull.

https://raw.githubusercontent.com/SewanDevs/mutextree/master-github/images/mutextree-step5.png

Interface targeted to be exactly like threading.Lock.

The mutex tree is actually designed to use redis and python-redis-lock but the locking backend may be changed.

Usage

To use mutextree with the redis locks back end, simply instanciate a redis client and create your lock. The redis client should be strict and decode responses itself.

import redis
from mutextree import RedisLockBackend, TreeLock

redis_client = redis.StrictRedis(decode_responses=True)
redis_lock_backend = RedisLockBackend(redis_client)
lock = TreeLock(redis_lock_backend, ["nodeA"], expire=10, timeout=10, id=1)
try:
    lock.acquire()
    # do things
finally:
    lock.release()

You can use it as a context manager or a decorator:

import redis
from mutextree import RedisLockBackend, TreeLock, tree_lock

redis_client = redis.StrictRedis(decode_responses=True)
redis_lock_backend = RedisLockBackend(redis_client)

with TreeLock(redis_lock_backend, ["nodeA"], expire=10):
    # do things
    pass
    # release will be automatically called

# Or with a decorator
@tree_lock(redis_lock_backend, ["nodeA"], expire=10)
def protected_function():
    # do things
    pass
    # release will be automatically called

Lock has the same basic interface as threading.Lock() with some more methods: acquire, release, refresh.

Tests

Tests should be run under python 2.7 and python 3.6 to tests everything

$ pip install tox
$ tox -e py27,py36

Coverage reports will be the merge of the coverage for py27 and py36.

Release files for mutextree 1.2.3

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

Source distribution (sdist)

Source distribution for mutextree 1.2.3
File Size Uploaded
mutextree-1.2.3.tar.gz 189.5 kB Details

Release files / mutextree-1.2.3.tar.gz

Download URL mutextree-1.2.3.tar.gz
Size 189.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e0e4d32a85af51dd3db7bc94f26febd75bbac803a1509f53a3614c0fa483892c
BLAKE2b-256 checksum
How to use checksums
cd9c675bf05e768d60f3e1006f3d476d6db452c714e24d85160c47e934240f49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/2.7.15

Release history Release notifications | RSS feed

This release

1.2.3 This release

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