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
This commit is contained in:
parent
080b019efc
commit
9374b1284e
1 changed files with 7 additions and 2 deletions
|
@ -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)
|
||||
|
||||
try:
|
||||
await asyncio.gather(*tasks)
|
||||
except asyncio.TimeoutError:
|
||||
logging.warning("Timeout while processing shipments")
|
||||
|
||||
await asyncio.sleep(self.loop_interval)
|
||||
|
||||
def start(self):
|
||||
|
|
Loading…
Reference in a new issue