momotor.shared.state#

The momotor.shared.state module is used by the workers to maintain a shared state.

Local workers use the LocalState implementation as a ‘dummy’ implementation, since local workers do not need access to any shared state.

Workers connected to the broker use a subclass of the abstract StateABC that implements connecting to the broker to exchange the state.

The implementation on the broker subclasses LocalState.

Class documentation#

class momotor.shared.state.StateABC#

Abstract base class to implement shared locks

get_lock(key, *, exclusive=True)#

Context manager to get a lock. The lock is held while the context is active.

The lock is an ‘exclusive’ or ‘read/write’ lock:

  • Many processes can hold the lock with exclusive==False (read mode), but

  • Only one process can hold the lock with exclusive==True (write mode). No other processes can hold the lock while one process has it locked exclusively.

Parameters:
  • key (str) – global name for the lock

  • exclusive (bool) – get an exclusive lock

Return type:

AsyncContextManager[None]

Returns:

Raises:

LockFailed

async test_lock(key, *, exclusive=True)#

Test the lock. Returns True if the lock is currently not being held.

Not that this is very transient information. The lock could be acquired immediately after this method returns.

Parameters:
  • key (str) – global name for the lock

  • exclusive (bool) – get an exclusive lock

Return type:

bool

Returns:

class momotor.shared.state.LocalState#

Reference implementation of StateABC for local use.

Uses a WeakValueDictionary of momotor.shared.exlock.ExLock objects.

Produces debug logging information on the momotor.shared.state.local logger

get_lock(key, *, exclusive=True)#

Context manager to get a lock. The lock is held while the context is active.

The lock is an ‘exclusive’ or ‘read/write’ lock:

  • Many processes can hold the lock with exclusive==False (read mode), but

  • Only one process can hold the lock with exclusive==True (write mode). No other processes can hold the lock while one process has it locked exclusively.

Parameters:
  • key (str) – global name for the lock

  • exclusive (bool) – get an exclusive lock

Return type:

AsyncContextManager[None]

Returns:

Raises:

LockFailed

async test_lock(key, *, exclusive=True)#

Test the lock. Returns True if the lock is currently not being held.

Not that this is very transient information. The lock could be acquired immediately after this method returns.

Parameters:
  • key (str) – global name for the lock

  • exclusive (bool) – get an exclusive lock

Return type:

bool

Returns:

class momotor.shared.state.LockFailed#

Exception to indicate a StateABC subclass could not acquire the lock