Python implementation of mutex using redis
pip install redismutex
To apply mutex to a block of code, first create a redis connection object using redis.StrictRedis
. This connection object is necessary as all the mutex keys are stored in redis. Now use the RedisMutex
to create a mutex object.
import redis
from redismutex import RedisMutex
conn = redis.StrictRedis(host='localhost', port=6379, db=1)
mutex = RedisMutex(conn)
mutex_key = 'YOUR-MUTEX-KEY'
with mutex.acquire_lock(mutex_key):
# your blocking code
# goes here...
print(mutex.key, mutex.value)
View detailed docs at py-redismutex.readthedocs.io