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:

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.Lock per 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 acquire method.

Parameters:
  • key (TypeVar(KT, bound= Hashable)) – Key that identifies the lock within this set.

  • 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:

SetLock

Returns:

The lock for key.

class momotor.shared.lockset.ExLockSet(set_name)

A set of reader-writer locks, one ExLock per 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 acquire method.

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]]) – True for exclusive (write) access, False for 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:

SetLock

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() and ExLockSet.get(). Use an instance as an async context manager, or call acquire() and release() directly.

async acquire()

Acquire the underlying lock.

Wait until the lock becomes available, then hold it through this instance. locked is True after this method returns.

release()

Release the underlying lock.

Call this method only after acquire() has completed on this instance. locked is False after this method returns.

property exclusive: bool

Whether this lock requests exclusive (write) access.

property locked: bool

Whether this lock is currently acquired through this instance.