get_lock¶
-
lockmgr.lockmgr.
get_lock
(name, expires: Optional[int] = 600, locked_by: str = None, lock_process: int = None) → lockmgr.models.Lock[source]¶ READ THIS: It’s best to use
LockMgr
as it automatically handles locking and unlocking usingwith
.Calls
clean_locks()
to remove any expired locks, checks for any existing locks using a FOR UPDATE transaction, then attempts to obtain a lock using the Lock modelpayments.models.Lock
If
name
is already locked, thenLocked
will be raised.Otherwise, if it was successfully locked, a
payments.models.Lock
object for the requested lock name will be returned.Usage:
>>> try: # Obtain a lock on 'mylock', with an automatic expiry of 60 seconds. ... mylock = get_lock('mylock', 60) ... print('Successfully locked mylock') ... except Locked as e: ... print('Failed to lock. Reason: ', type(e), str(e)) ... finally: # Regardless of whether there was an exception or not, remember to remove the lock! ... print('Removing lock on "mylock"') ... unlock(mylock)
- Parameters
name (str) – A unique name to identify your lock
expires (int) – (Default: 600 sec) How long before this lock is considered stale and forcefully released? Set this to
0
for a lock which will never expire (must manually callunlock()
)locked_by (str) – (Default: system hostname) What server/app is trying to obtain this lock?
lock_process (int) – (Optional) The process ID requesting the lock
- Raises
Locked – If the requested lock
name
is already locked elsewhere,Locked
will be raised- Return Lock lock
If successfully locked, will return the
payments.models.Lock
of the requested lock.