Skip to main content

mutexcache-python

A small utility library for dynamically creating mutexes based on cache keys.

Use case

Say you're implementing a graphql server, with object fields which asynchronously resolve independently of each other. Multiple fields perform the same operation, and thus should use a mutex and some basic caching to ensure that the database query happens at most once. If you have an array of these objects, suddenly they're all using the same mutex, which can degrade performance.

Instead of using one mutex to rule them all, dynamically create multiple short-lived mutexes which each object can use independently of other resolving objects. By using the same cache key for the mutexes as you would for your cache check, you can almost transparently use dynamically created mutexes without worrying about performance or allocation/deallocation of mutexes.

With mutexcache, if a mutex associated with a cache key is already stored, then it will be returned. Otherwise, a new mutex will silently be created, stored for future use, and returned.

Installation

pip3 install mutexcache

Usage

MutexCache.get() returns threading.Lock objects.

from mutexcache import MutexCache

mutex_cache = MutexCache() # optionally, provide a ttl in seconds 

cache_key_a = "key_a"
cache_key_b = "key_b"

mut_a = mutex_cache.get(cache_key_a)
mut_b = mutex_cache.get(cache_key_b)

# [...do stuff with your Locks...]

Release files for mutexcache 0.1.1

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

Source distribution (sdist)

Source distribution for mutexcache 0.1.1
File Size Uploaded
mutexcache-0.1.1.tar.gz 2.3 kB Details

Release files / mutexcache-0.1.1.tar.gz

Download URL mutexcache-0.1.1.tar.gz
Size 2.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a33cd4978e1ce1672e1d3d7f584cd3dbf9ba3bf6ba1082f48f5bb828e4f6a596
BLAKE2b-256 checksum
How to use checksums
c438f56bbe69f308a9b9b6ea9a7cad0f520b4a42c380370b966e6b638a37621e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file

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