From 9374b1284ea293b95b2e0e9b29ce1153182ed19f Mon Sep 17 00:00:00 2001 From: Kumi Date: Thu, 24 Aug 2023 17:34:18 +0200 Subject: [PATCH] Update loop_interval in Tracker class and handle timeout while processing shipments - Update loop_interval to 60 seconds - Add loop_timeout to Tracker class with a value of 30 seconds - Wrap process_shipment calls in asyncio.wait_for with timeout set to loop_timeout value - Add try-except block to handle TimeoutError and log a warning message - Code changes aim to improve the handling of long-running shipments processing with a timeout mechanism in place --- classes/tracker.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/classes/tracker.py b/classes/tracker.py index a27209f..1d273e8 100644 --- a/classes/tracker.py +++ b/classes/tracker.py @@ -14,6 +14,7 @@ from pykeydelivery import KeyDelivery class Tracker: loop_interval = 60 + loop_timeout = 30 def __init__(self): logging.basicConfig( @@ -158,10 +159,14 @@ class Tracker: while True: tasks = [] for shipment in self.db.get_shipments(): - task = asyncio.create_task(asyncio.to_thread(self.process_shipment, shipment)) + task = asyncio.wait_for(asyncio.to_thread(self.process_shipment, shipment), timeout=self.loop_timeout) tasks.append(task) - await asyncio.gather(*tasks) + try: + await asyncio.gather(*tasks) + except asyncio.TimeoutError: + logging.warning("Timeout while processing shipments") + await asyncio.sleep(self.loop_interval) def start(self):