From 0c9362a2c0f73443c3573f95438f5637dc170961 Mon Sep 17 00:00:00 2001 From: Paulo Vital Date: Tue, 28 Nov 2023 15:53:39 +0100 Subject: [PATCH] refactor: Using context manager to handle threading.Lock Signed-off-by: Paulo Vital --- instana/collector/base.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/instana/collector/base.py b/instana/collector/base.py index ba2c73c3f..bdd767e7e 100644 --- a/instana/collector/base.py +++ b/instana/collector/base.py @@ -150,16 +150,11 @@ def prepare_and_report_data(self): Prepare and report the data payload. @return: Boolean """ - if env_is_test is False: - lock_acquired = self.background_report_lock.acquire(False) - if lock_acquired: - try: - payload = self.prepare_payload() - self.agent.report_data_payload(payload) - finally: - self.background_report_lock.release() - else: - logger.debug("prepare_and_report_data: Couldn't acquire lock") + if env_is_test: + return True + with self.background_report_lock: + payload = self.prepare_payload() + self.agent.report_data_payload(payload) return True def prepare_payload(self):