sqlalchemy_dlock.lock.mysql module¶
- class sqlalchemy_dlock.lock.mysql.MysqlAsyncSadLock(connection_or_session, key, **kwargs)[source]¶
Bases:
MysqlSadLockMixin,BaseAsyncSadLock[str,AsyncConnection|AsyncSession|async_scoped_session]Async IO version of
MysqlSadLock- Parameters:
key (
Any) –MySQL named lock requires the key given by string.
If
keyis not astr:convert –
Custom function to covert
keyto required data type.Example
def convert(value) -> str: # get a string key by `value` return the_string_covert_from_value
connection_or_session (AsyncConnection | AsyncSession | async_scoped_session)
- class sqlalchemy_dlock.lock.mysql.MysqlSadLock(connection_or_session, key, **kwargs)[source]¶
Bases:
MysqlSadLockMixin,BaseSadLock[str,Connection|Session|scoped_session]A distributed lock implemented by MySQL named-lock
Caution
To MySQL locking function, it is even possible for a given session to acquire multiple locks for the same name. Other sessions cannot acquire a lock with that name until the acquiring session releases all its locks for the name. When perform multiple
acquire()for a key on the same SQLAlchemy connection, latteracquire()will success immediately no wait and never block, it causes cascade lock instead!- Parameters:
connection_or_session (
Union[Connection,Session,scoped_session]) –BaseSadLock.connection_or_sessionkey (
Any) –BaseSadLock.key**kwargs – other named parameters pass to
BaseSadLockandMysqlSadLockMixin
- acquire(block=True, timeout=None, *args, **kwargs)[source]¶
Acquire the lock in blocking or non-blocking mode.
The implementation should provide the following behavior: :rtype:
boolWhen
blockisTrue(the default), the method blocks until the lock is in an unlocked state, then sets it to locked and returnsTrue.When
blockisFalse, the method call is non-blocking. If the lock is currently locked, it returnsFalse; otherwise, it sets the lock to locked state and returnsTrue.When invoked with a positive floating-point value for
timeout, it blocks for at most the specified number of seconds until the lock can be acquired.Invocations with a negative
timeoutvalue are equivalent to atimeoutof zero.When
timeoutisNone(the default), the timeout period is infinite. Thetimeoutparameter has no effect whenblockisFalseand is thus ignored.Returns
Trueif the lock has been acquired orFalseif the timeout period has elapsed.
- release()[source]¶
Release the lock.
Since the class is thread-local, this method cannot be called from another thread or process, nor can it be called from another connection. (Although PostgreSQL’s shared advisory lock supports this).
The implementation should provide the following behavior:
Reset the lock to unlocked state and return when the lock is currently locked.
Allow exactly one of any other threads blocked waiting for the lock to become unlocked to proceed.
Raise a
ValueErrorwhen invoked on an unlocked lock.Not return a value.
- class sqlalchemy_dlock.lock.mysql.MysqlSadLockMixin(*, key, convert=None, **kwargs)[source]¶
Bases:
AbstractLockMixin[KTV,str]A Mix-in class for MySQL named lock
- Parameters:
key (
TypeVar(KTV, bound=Any)) –MySQL named lock requires the key given by string.
If
keyis not astr:convert (
Optional[Callable[[TypeVar(KTV, bound=Any)],str]]) –Custom function to covert
keyto required data type.Example
def convert(value) -> str: # get a string key by `value` return the_string_covert_from_value