Synchronisation via Conditions
- class usim.Tracked(value: usim._basics.tracked.V)[source]
A mutable value whose changes are tracked to generate and trigger events
The purpose of a tracked value is to derive notification points on the fly. Comparison and mathematical expressions define and trigger events, respectively:
async def refill(coffee: Tracked[float]): while True: # boolean expression providing an event to wait for await (coffee < 0.1) print('Coffee is low! Initiating emergency refill!') # arithmetic expression triggering events of waiters await (coffee + 0.9) print('Coffee refilled! Emergency resolved!')
The purpose of
Tracked
is to make operations asynchronous - the actual operations are taken from the wrapped value. This dictates both the availability and effect of operations.Tracked
does not require its underlyingvalue
to be mutable. Instead, the underlyingvalue
is replaced whenTracked
changes - for example,await (tracked + 5)
is a shorthand forawait tracked.set(tracked.value + 5)
. This works both for immutable types, such asint
, and mutable types, such aslist
.Tracked
assumes sole responsibility for changingvalue
. This means thatvalue
should be changed only viaset()
or async operators, such asawait (tracked + 3)
. Circumventing this to change a mutablevalue
directly preventsTracked
from detecting the change and triggering events.- property value: usim._basics.tracked.V
The current value