sqlalchemy_dlock.lock.base module¶
- class sqlalchemy_dlock.lock.base.AbstractLockMixin(*, key, convert=None, **kwargs)[source]¶
Bases:
Generic[KeyTV,ActualKeyTV],ABC- Parameters:
key (KeyTV)
convert (Callable[[KeyTV], ActualKeyTV] | None)
- property actual_key: ActualKeyTV¶
- class sqlalchemy_dlock.lock.base.BaseAsyncSadLock(connection_or_session, key, /, contextual_timeout=None, **kwargs)[source]¶
Bases:
AbstractLockMixin,Generic[KeyTV,AsyncConnectionTV],_local,ABCAsync version of
BaseSadLock- Parameters:
- property connection_or_session: AsyncConnectionTV¶
- property key: KeyTV¶
- class sqlalchemy_dlock.lock.base.BaseSadLock(connection_or_session, key, /, contextual_timeout=None, **kwargs)[source]¶
Bases:
AbstractLockMixin,Generic[KeyTV,ConnectionTV],_local,ABCBase class of database lock implementation
Note
It’s Thread-Local (
threading.local)It’s an abstract class, do not manual instantiate
The
acquire()andrelease()methods can be used as context managers for awithstatement.acquire()will be called when the block is entered, andrelease()will be called when the block is exited. Hence, the following snippet:with some_lock: # do something... pass
is equivalent to:
some_lock.acquire() try: # do something... pass finally: some_lock.release()
Note
A
TimeoutErrorwill be thrown if acquire timeout inwithstatement.- Parameters:
connection_or_session (
TypeVar(ConnectionTV, bound=Connection|Session|scoped_session)) – Connection or Session object SQL locking functions will be invoked on itkey (
TypeVar(KeyTV)) – ID or name of the SQL locking functioncontextual_timeout (
float|int|None) –Timeout(seconds) for Context Managers.
When called in a
withstatement, the new created lock object will pass it totimeoutargument ofBaseSadLock.acquire().Attention
ONLY affects
withstatements.Example
try: with create_sadlock(conn, k, contextual_timeout=5) as lck: # do something... pass except TimeoutError: # can not acquire after 5 seconds pass
- final acquire(block=True, timeout=None, *args, **kwargs)[source]¶
Acquire the lock in blocking or non-blocking mode.
The implementation (
do_acquire()) should provide the following behavior:When
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.
- final close(*args, **kwargs)[source]¶
Same as
release()Except that the
ValueErroris NOT raised when invoked on an unlocked lock.An invocation of this method is equivalent to:
if not some_lock.locked: some_lock.release()
This method maybe useful together with
contextlib.closing(), when we need awithstatement, but don’t want it to acquire at the beginning of the block.Example
# ... from contextlib import closing from sqlalchemy_dlock import create_sadlock # ... with closing(create_sadlock(some_connection, some_key)) as lock: # will **NOT** acquire at the begin of with-block assert not lock.locked # ... # lock when need lock.acquire() assert lock.locked # ... # `close` will be called at the end with-block assert not lock.locked
- property connection_or_session: ConnectionTV¶
Connection or Session object SQL locking functions will be invoked on it
It returns
connection_or_sessionparameter of the class’s constructor.
- property key: KeyTV¶
ID or name of the SQL locking function
It returns
keyparameter of the class’s constructor
- final release(*args, **kwargs)[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 (
do_release()) 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.