Package redis :: Module client :: Class Lock
[frames] | no frames]

Class Lock

source code

object --+
         |
        Lock

A shared, distributed Lock. Using Redis for locking allows the Lock to be shared across processes and/or machines.

It's left to the user to resolve deadlock issues and make sure multiple clients play nicely together.

Instance Methods
 
__init__(self, redis, name, timeout=None, sleep=0.1)
Create a new Lock instnace named ``name`` using the Redis client supplied by ``redis``.
source code
 
__enter__(self) source code
 
__exit__(self, exc_type, exc_value, traceback) source code
 
acquire(self, blocking=True)
Use Redis to hold a shared, distributed lock named ``name``.
source code
 
release(self)
Releases the already acquired lock
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables
  LOCK_FOREVER = 2147483649.0
Properties

Inherited from object: __class__

Method Details

__init__(self, redis, name, timeout=None, sleep=0.1)
(Constructor)

source code 

Create a new Lock instnace named ``name`` using the Redis client supplied by ``redis``.

``timeout`` indicates a maximum life for the lock. By default, it will remain locked until release() is called.

``sleep`` indicates the amount of time to sleep per loop iteration when the lock is in blocking mode and another client is currently holding the lock.

Note: If using ``timeout``, you should make sure all the hosts that are running clients have their time synchronized with a network time service like ntp.

Overrides: object.__init__

acquire(self, blocking=True)

source code 

Use Redis to hold a shared, distributed lock named ``name``. Returns True once the lock is acquired.

If ``blocking`` is False, always return immediately. If the lock was acquired, return True, otherwise return False.