projects
/
python_utils.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
52290f9
)
Update comments.
author
Scott
<
[email protected]
>
Thu, 13 Jan 2022 04:56:31 +0000
(20:56 -0800)
committer
Scott
<
[email protected]
>
Thu, 13 Jan 2022 04:56:31 +0000
(20:56 -0800)
state_tracker.py
patch
|
blob
|
history
diff --git
a/state_tracker.py
b/state_tracker.py
index 4f77ff42241474d214f222c428ae0f60e1da1e51..9ce61e3ac7a51c7cacdc2cc7e34dea02985078de 100644
(file)
--- a/
state_tracker.py
+++ b/
state_tracker.py
@@
-20,8
+20,8
@@
class StateTracker(ABC):
invoked via the heartbeat() method. This method, in turn, invokes
update() with update_ids according to a schedule / periodicity
provided to the c'tor.
invoked via the heartbeat() method. This method, in turn, invokes
update() with update_ids according to a schedule / periodicity
provided to the c'tor.
- """
+ """
def __init__(self, update_ids_to_update_secs: Dict[str, float]) -> None:
"""The update_ids_to_update_secs dict parameter describes one or more
update types (unique update_ids) and the periodicity(ies), in
def __init__(self, update_ids_to_update_secs: Dict[str, float]) -> None:
"""The update_ids_to_update_secs dict parameter describes one or more
update types (unique update_ids) and the periodicity(ies), in
@@
-30,6
+30,7
@@
class StateTracker(ABC):
Note that, when more than one update is overdue, they will be
invoked in order by their update_ids so care in choosing these
identifiers may be in order.
Note that, when more than one update is overdue, they will be
invoked in order by their update_ids so care in choosing these
identifiers may be in order.
+
"""
self.update_ids_to_update_secs = update_ids_to_update_secs
self.last_reminder_ts: Dict[str, Optional[datetime.datetime]] = {}
"""
self.update_ids_to_update_secs = update_ids_to_update_secs
self.last_reminder_ts: Dict[str, Optional[datetime.datetime]] = {}
@@
-49,6
+50,7
@@
class StateTracker(ABC):
The now param is the approximate current timestamp and the
last_invocation param is the last time you were invoked (or
None on the first invocation)
The now param is the approximate current timestamp and the
last_invocation param is the last time you were invoked (or
None on the first invocation)
+
"""
pass
"""
pass
@@
-64,6
+66,7
@@
class StateTracker(ABC):
Setting force_all_updates_to_run will invoke all updates
(ordered by update_id) immediately ignoring whether or not
they are due.
Setting force_all_updates_to_run will invoke all updates
(ordered by update_id) immediately ignoring whether or not
they are due.
+
"""
self.now = datetime.datetime.now(tz=pytz.timezone("US/Pacific"))
for update_id in sorted(self.last_reminder_ts.keys()):
"""
self.now = datetime.datetime.now(tz=pytz.timezone("US/Pacific"))
for update_id in sorted(self.last_reminder_ts.keys()):
@@
-101,13
+104,15
@@
class AutomaticStateTracker(StateTracker):
"""Just like HeartbeatCurrentState but you don't need to pump the
heartbeat; it runs on a background thread. Call .shutdown() to
terminate the updates.
"""Just like HeartbeatCurrentState but you don't need to pump the
heartbeat; it runs on a background thread. Call .shutdown() to
terminate the updates.
- """
+ """
@background_thread
def pace_maker(self, should_terminate) -> None:
"""Entry point for a background thread to own calling heartbeat()
at regular intervals so that the main thread doesn't need to do
@background_thread
def pace_maker(self, should_terminate) -> None:
"""Entry point for a background thread to own calling heartbeat()
at regular intervals so that the main thread doesn't need to do
- so."""
+ so.
+
+ """
while True:
if should_terminate.is_set():
logger.debug('pace_maker noticed event; shutting down')
while True:
if should_terminate.is_set():
logger.debug('pace_maker noticed event; shutting down')
@@
-138,6
+143,7
@@
class AutomaticStateTracker(StateTracker):
def shutdown(self):
"""Terminates the background thread and waits for it to tear down.
This may block for as long as self.sleep_delay.
def shutdown(self):
"""Terminates the background thread and waits for it to tear down.
This may block for as long as self.sleep_delay.
+
"""
logger.debug(
'Setting shutdown event and waiting for background thread.'
"""
logger.debug(
'Setting shutdown event and waiting for background thread.'
@@
-148,7
+154,26
@@
class AutomaticStateTracker(StateTracker):
class WaitableAutomaticStateTracker(AutomaticStateTracker):
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],
def __init__(
self,
update_ids_to_update_secs: Dict[str, float],