redis_lock

exception redis_lock.AlreadyAcquired[source]
exception redis_lock.AlreadyStarted[source]
exception redis_lock.InvalidTimeout[source]
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, call super().__init__() then set self._lock_renewal_interval to 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
get_owner_id()[source]
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.

classmethod register_scripts(redis_client)[source]
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()[source]

Forcibly deletes the lock. Use this with care.

reset_all_script = None
reset_script = None
unlock_script = None
exception redis_lock.NotAcquired[source]
exception redis_lock.NotExpirable[source]
exception redis_lock.TimeoutNotUsable[source]
exception redis_lock.TimeoutTooLarge[source]
redis_lock.reset_all(redis_client)[source]

Forcibly deletes all locks if its remains (like a crash reason). Use this with care.

Parameters:

redis_client – An instance of StrictRedis.