From 94d3a659738cf65b419c7901a30fb68748bdfeec Mon Sep 17 00:00:00 2001 From: CamDavidsonPilon Date: Thu, 5 Dec 2024 10:37:40 -0500 Subject: [PATCH] adding an event so we can exit early if requested --- config.dev.ini | 4 ++-- pioreactor/background_jobs/base.py | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/config.dev.ini b/config.dev.ini index 8a8a9880..9c29cea4 100644 --- a/config.dev.ini +++ b/config.dev.ini @@ -39,7 +39,7 @@ use_rpm=True duration_between_updates_seconds=23 post_delay_duration=0.25 pre_delay_duration=2.0 -enable_dodging_od=False +enable_dodging_od=True [stirring.pid] @@ -54,7 +54,7 @@ Kd=0.0 [od_reading.config] # how many samples should the ADC publish per second? -samples_per_second=0.2 +samples_per_second=0.03 pd_reference_ema=0.4 diff --git a/pioreactor/background_jobs/base.py b/pioreactor/background_jobs/base.py index df1fc639..10d54c94 100644 --- a/pioreactor/background_jobs/base.py +++ b/pioreactor/background_jobs/base.py @@ -1048,6 +1048,7 @@ def __init__(self, *args, source="app", **kwargs) -> None: ) # placeholder? self.add_to_published_settings("enable_dodging_od", {"datatype": "boolean", "settable": True}) self.add_to_published_settings("currently_dodging_od", {"datatype": "boolean", "settable": False}) + self._event_is_dodging_od = threading.Event() def __post__init__(self): self.set_enable_dodging_od( @@ -1063,11 +1064,13 @@ def __post__init__(self): def set_currently_dodging_od(self, value: bool): self.currently_dodging_od = value if self.currently_dodging_od: + self._event_is_dodging_od.clear() self.initialize_dodging_operation() # user defined self._action_to_do_before_od_reading = self.action_to_do_before_od_reading self._action_to_do_after_od_reading = self.action_to_do_after_od_reading self._setup_timer() else: + self._event_is_dodging_od.set() self.initialize_continuous_operation() # user defined self._action_to_do_before_od_reading = _noop self._action_to_do_after_od_reading = _noop @@ -1135,7 +1138,12 @@ def sneak_in(ads_interval: float, post_delay: float, pre_delay: float) -> None: "samples_per_second is too high, or post_delay is too high, or pre_delay is too high, or action_to_do_after_od_reading takes too long." ) - sleep(ads_interval - self.OD_READING_DURATION - (post_delay + pre_delay) - action_after_duration) + if self.state != self.READY: + return + + self._event_is_dodging_od.wait( + ads_interval - self.OD_READING_DURATION - (post_delay + pre_delay) - action_after_duration + ) if self.state != self.READY: return