From dd5886c3c006e388283df50cc87addeffb3b2b52 Mon Sep 17 00:00:00 2001 From: Boris Ranto Date: Wed, 25 Nov 2020 20:49:48 +0100 Subject: [PATCH] mgr/prometheus: use threading.Event instead of sleep This allows us to avoid waiting for the sleep to finish when waiting for the thread to finish. Signed-off-by: Boris Ranto --- src/pybind/mgr/prometheus/module.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/pybind/mgr/prometheus/module.py b/src/pybind/mgr/prometheus/module.py index c5ec065853b25..359578e5415b3 100644 --- a/src/pybind/mgr/prometheus/module.py +++ b/src/pybind/mgr/prometheus/module.py @@ -188,6 +188,7 @@ class MetricCollectionThread(threading.Thread): # type: (Module) -> None self.mod = module self.active = True + self.event = threading.Event() super(MetricCollectionThread, self).__init__(target=self.collect) def collect(self): @@ -202,7 +203,7 @@ class MetricCollectionThread(threading.Thread): except Exception as e: # Log any issues encountered during the data collection and continue self.mod.log.exception("failed to collect metrics:") - time.sleep(self.mod.scrape_interval) + self.event.wait(self.mod.scrape_interval) continue duration = time.time() - start_time @@ -227,13 +228,14 @@ class MetricCollectionThread(threading.Thread): self.mod.collect_cache = data self.mod.collect_time = duration - time.sleep(sleep_time) + self.event.wait(sleep_time) else: self.mod.log.error('No MON connection') - time.sleep(self.mod.scrape_interval) + self.event.wait(self.mod.scrape_interval) def stop(self): self.active = False + self.event.set() class Module(MgrModule): COMMANDS = [ -- 2.47.3