Shared state¶
Workers sometimes share data that only one of them may change at a time.
The shared package defines the lock interface for that data as
momotor.shared.state.StateABC.
This module implements that interface for workers that connect to the
broker. The broker holds the lock, so workers in different processes and
on different machines exclude each other correctly.
SharedState takes a connected worker stub:
from momotor.rpc.shared import SharedState
state = SharedState(worker_stub)
async with state.get_lock('tool-cache', exclusive=True):
... # only one worker runs this block at a time
With exclusive=False the lock is shared: many workers can hold it at
the same time, until one asks for exclusive access.
If the broker refuses a lock, get_lock raises
momotor.shared.state.LockFailed.
For code that must also run without a broker, the shared package has a
process-local implementation. See
the state documentation in the
shared package.
API reference¶
- class momotor.rpc.shared.SharedState(stub)¶
An implementation of
momotor.shared.state.StateABCfor use by workers.Produces log messages on the
momotor.rpc.sharedlogger.- Parameters:
stub (
WorkerStub) – The connected worker stub
- 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 when exclusive is
False(read mode), butOnly one process can hold the lock when exclusive is
True(write mode). No other processes can hold the lock while one process has it locked exclusively.
- Parameters:
- Return type:
- Returns:
An async context manager that holds the lock and yields
None.- Raises:
- async test_lock(key, *, exclusive=True)¶
Test the lock. Returns
Trueif the lock is currently being held.Note that this is very transient information. The lock could be acquired immediately after this method returns.