renew_lock¶
-
lockmgr.lockmgr.
renew_lock
(lock: Union[str, lockmgr.models.Lock], expires: int = 600, add_time: bool = False, **kwargs) → lockmgr.models.Lock[source]¶ Renew an existing lock for more expiry time.
Note: This function will NOT reduce a lock’s expiry time, only lengthen. If
add_time
isFalse
, and the new expiration timeexpires
is shorter than the lock’s existing expiration time, then the lock’s expiry time will be left untouched.Example - Renew an existing lock:
>>> lk = get_lock('my_app:somelock', expires=10) >>> sleep(5) >>> lk = renew_lock(lk, 20) # Change the expiry time to 20 seconds from now >>> sleep(15) >>> is_locked('my_app:somelock') # 15 seconds later, the lock is still locked True
Example - Try to renew, but get a new lock if it’s already been released:
>>> lk = get_lock('my_app:somelock', expires=5) >>> sleep(10) >>> lk = renew_lock(lk, 20, create=True) # If the lock is expired/non-existant, make a new lock >>> sleep(15) >>> is_locked('my_app:somelock') # 15 seconds later, the lock is still locked True
- Parameters
lock (Lock) – Name of the lock to renew
lock – A
Lock
object to renewexpires (int) – (Default: 600) If not add_time, then this is the new expiration time in seconds from now. If add_time, then this many seconds will be added to the expiration time of the lock.
add_time (bool) – (Default:
False
) If True, thenexpires
seconds will be added to the existing lock expiration time, instead of setting the expiration time tonow + expires
- Key bool create
(Default:
False
) If True, then create a new lock if it doesn’t exist / already expired.- Key str locked_by
(Default: system hostname) What server/app is trying to obtain this lock?
- Key int lock_process
(Optional) The process ID requesting the lock
- Raises
LockNotFound – Raised if the requested
lock
doesn’t exist / is already expired andcreate
is False.- Return Lock lock
The
Lock
object which was renewed