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
Trackedis to make operations asynchronous - the actual operations are taken from the wrapped value. This dictates both the availability and effect of operations.Trackeddoes not require its underlyingvalueto be mutable. Instead, the underlyingvalueis replaced whenTrackedchanges - 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.Trackedassumes sole responsibility for changingvalue. This means thatvalueshould be changed only viaset()or async operators, such asawait (tracked + 3). Circumventing this to change a mutablevaluedirectly preventsTrackedfrom detecting the change and triggering events.- property value: usim._basics.tracked.V¶
The current value