sqlalchemy_dlock.impl.mysql module¶
- class sqlalchemy_dlock.impl.mysql.SessionLevelLock(connection_or_session: Union[sqlalchemy.engine.base.Connection, sqlalchemy.orm.session.Session, sqlalchemy.orm.scoping.scoped_session], key, *, convert: Optional[Callable[[Any], str]] = None, **_)¶
Bases:
sqlalchemy_dlock.sessionlevellock.AbstractSessionLevelLock
MySQL named-lock
MySQL named lock requires the key given by string.
If key is not a
str
:When
int
orfloat
, the constructor will force convert it tostr
:key = str(key)
When
bytes
, the constructor tries to decode it with default encoding:key = key.decode()
Or you can specify a convert function to that argument. The function is like:
def convert(val: Any) -> str: # do something ... return string
- acquire(block: bool = True, timeout: Optional[Union[float, int]] = None, **_) bool ¶
Acquire a lock, blocking or non-blocking.
With the block argument set to
True
(the default), the method call will block until the lock is in an unlocked state, then set it to locked and returnTrue
.With the block argument set to
False
, the method call does not block. If the lock is currently in a locked state, returnFalse
; otherwise set the lock to a locked state and returnTrue
.When invoked with a positive, floating-point value for timeout, block for at most the number of seconds specified by timeout as long as the lock can not be acquired. Invocations with a negative value for timeout are equivalent to a timeout of zero. Invocations with a timeout value of
None
(the default) set the timeout period to infinite. The timeout argument has no practical implications if the block argument is set toFalse
and is thus ignored. ReturnsTrue
if the lock has been acquired orFalse
if the timeout period has elapsed.
- release(**_)¶
Release a lock.
Since the class is thread-local, this cannot be called from other thread or process, and also can not be called from other connection (PostgreSQL’s shared advisory lock supports so, but we haven’t).
When the lock is locked, reset it to unlocked, and return. If any other threads are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed.
When invoked on an unlocked lock, a
ValueError
is raised.There is no return value.