From 32e5907d8c26b9e0bc7dbf8e4c15215164dcb8f0 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 (cherry picked from commit dd5886c3c006e388283df50cc87addeffb3b2b52) --- 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 5008fcc8ece02..f5684f257ef54 100644 --- a/src/pybind/mgr/prometheus/module.py +++ b/src/pybind/mgr/prometheus/module.py @@ -189,6 +189,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): @@ -203,7 +204,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.39.5