sqlalchemy_dlock.lock.mysql module¶
- class sqlalchemy_dlock.lock.mysql.MysqlSadLockMixin(*, key: TKey, convert: Callable[[TKey], str] | None = None, **kwargs)[source]¶
Bases:
objectA Mix-in class for MySQL named lock
- class sqlalchemy_dlock.lock.mysql.MysqlSadLock(connection_or_session: Connection | Session | scoped_session, key, **kwargs)[source]¶
Bases:
MysqlSadLockMixin,BaseSadLock[str]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 (Connection | Session | scoped_session) –
BaseSadLock.connection_or_sessionkey –
BaseSadLock.key**kwargs – other named parameters pass to
BaseSadLockandMysqlSadLockMixin
- acquire(block: bool = True, timeout: float | int | None = None, *args, **kwargs) bool[source]¶
Acquire a lock, blocking or non-blocking.
With the
blockargument set toTrue(the default), the method call will block until the lock is in an unlocked state, then set it to locked and returnTrue.With the
blockargument set toFalse, 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. Thetimeoutparameter has no practical implications if theblockargument is set toFalseand is thus ignored. ReturnsTrueif the lock has been acquired orFalseif the timeout period has elapsed.
- release()[source]¶
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. (Although PostgreSQL’s shared advisory lock supports so).
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
ValueErroris raised.There is no return value.