Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

Dynamically created, cached mutexes in Python.

Project description

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...]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for mutexcache, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size mutexcache-0.1.1.tar.gz (2.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page