redis_lock¶
- class redis_lock.Lock(redis_client, name, expire=None, id=None, auto_renewal=False, signal_expire=1000, blocking=True)[source]¶
A Lock context manager implemented via redis SETNX/BLPOP.
- __init__(redis_client, name, expire=None, id=None, auto_renewal=False, signal_expire=1000, blocking=True)[source]¶
- Parameters:
redis_client – An instance of
StrictRedis.name – The name (redis key) the lock should have.
expire – The lock expiry time in seconds. If left at the default (None) the lock will not expire.
id –
The ID (redis value) the lock should have. A random value is generated when left at the default.
Note that if you specify this then the lock is marked as “held”. Acquires won’t be possible.
auto_renewal –
If set to
True, Lock will automatically renew the lock so that it doesn’t expire for as long as the lock is held (acquire() called or running in a context manager).Implementation note: Renewal will happen using a daemon thread with an interval of
expire*2/3. If wishing to use a different renewal time, subclass Lock, callsuper().__init__()then setself._lock_renewal_intervalto your desired interval.signal_expire – Advanced option to override signal list expiration in milliseconds. Increase it for very slow clients. Default:
1000.blocking – Boolean value specifying whether lock should be blocking or not. Used in __enter__ method.
- acquire(blocking=True, timeout=None)[source]¶
- Parameters:
blocking – Boolean value specifying whether lock should be blocking or not.
timeout – An integer value specifying the maximum number of seconds to block.
- blocking = None¶
- extend(expire=None)[source]¶
Extends expiration time of the lock.
- Parameters:
expire – New expiration time. If
None- expire provided during lock initialization will be taken.
- extend_script = None¶
- property id¶
- locked()[source]¶
Return true if the lock is acquired.
Checks that lock with same name already exists. This method returns true, even if lock have another id.
- release()[source]¶
Releases the lock, that was acquired with the same object.
Note
If you want to release a lock that you acquired in a different place you have two choices:
Use
Lock("name", id=id_from_other_place).release()Use
Lock("name").reset()
- reset_all_script = None¶
- reset_script = None¶
- unlock_script = None¶