+
+
+class WaitableAutomaticStateTracker(AutomaticStateTracker):
+ """This is an AutomaticStateTracker that exposes a wait method which
+ will block the calling thread until the state changes with an
+ optional timeout. The caller should check the return value of
+ wait; it will be true if something changed and false if the wait
+ simply timed out. If the return value is true, the instance
+ should be reset() before wait is called again.
+
+ Example usage:
+
+ detector = waitable_presence.WaitableAutomaticStateSubclass()
+ while True:
+ changed = detector.wait(timeout=60 * 5)
+ if changed:
+ detector.reset()
+ # Figure out what changed and react
+ else:
+ # Just a timeout; no need to reset. Maybe do something
+ # else before looping up into wait again.
+
+ """
+ def __init__(
+ self,
+ update_ids_to_update_secs: Dict[str, float],
+ *,
+ override_sleep_delay: Optional[float] = None,
+ ) -> None:
+ self._something_changed = threading.Event()
+ super().__init__(update_ids_to_update_secs,
+ override_sleep_delay=override_sleep_delay)
+
+ def something_changed(self):
+ self._something_changed.set()
+
+ def did_something_change(self) -> bool:
+ return self._something_changed.is_set()
+
+ def reset(self):
+ self._something_changed.clear()
+
+ def wait(self,
+ *,
+ timeout=None):
+ return self._something_changed.wait(
+ timeout=timeout
+ )