momotor.shared.lockset¶
A lock set manages one lock per key. Use it when the set of things to lock is not fixed in advance, for example one lock per asset or per remote connection. The set creates a lock the first time a key is used and reuses it after that. Locks that nobody references anymore are removed automatically.
Two set types exist:
LockSetgives a plainasyncio.Lockper key. One holder at a time.ExLockSetgives a reader-writer lock per key, based onExLock. Each request chooses shared or exclusive access. See momotor.shared.exlock.
from momotor.shared.lockset import ExLockSet, LockSet
locks = LockSet('asset')
async with await locks.get('asset-42'):
... # only one holder for 'asset-42' at a time
rw_locks = ExLockSet('asset')
async with await rw_locks.get('asset-42', exclusive=False):
... # shared read access for 'asset-42'
get returns a SetLock. Use it as an
async context manager as in the example, or call its acquire and
release methods directly.
For ExLockSet, the exclusive
argument can also be an async callable. The set calls it with the key and a
boolean that tells whether the lock was just created. The callable returns
the access mode to use. This supports patterns such as “the creator writes,
everyone else reads”.
API reference¶
- class momotor.shared.lockset.LockSet(set_name)¶
A set of plain async locks, one
asyncio.Lockper key.- Parameters:
set_name (
str) – Name of the set, used in debug log messages.
- async get(key, *, caller_name=None, log_key=None)¶
Get the lock for key, creating it on first use.
The returned lock is not acquired yet. Use it as an async context manager or call its
acquiremethod.- Parameters:
- Return type:
- Returns:
The lock for key.
- class momotor.shared.lockset.ExLockSet(set_name)¶
A set of reader-writer locks, one
ExLockper key.- Parameters:
set_name (
str) – Name of the set, used in debug log messages.
- async get(key, exclusive, *, caller_name=None, log_key=None)¶
Get the lock for key, creating it on first use.
The returned lock is not acquired yet. Use it as an async context manager or call its
acquiremethod.- Parameters:
key (
TypeVar(KT, bound=Hashable)) – Key that identifies the lock within this set.exclusive (
bool|Callable[[TypeVar(KT, bound=Hashable),bool],Coroutine[None,None,bool]]) –Truefor exclusive (write) access,Falsefor shared (read) access. An async callable is also accepted. The set calls it with the key and a boolean that tells whether the lock was just created, and uses the returned boolean as the access mode.caller_name (
str|None) – Name to use in debug log messages. Detected from the caller when omitted.log_key (
str|None) – Text to log instead of the key itself.
- Return type:
- Returns:
The lock for key.
- class momotor.shared.lockset.SetLock(lock_ref, lock, exclusive, log_label)¶
One lock from a lock set, ready to acquire.
Instances come from
LockSet.get()andExLockSet.get(). Use an instance as an async context manager, or callacquire()andrelease()directly.- async acquire()¶
Acquire the underlying lock.
Wait until the lock becomes available, then hold it through this instance.
lockedisTrueafter this method returns.