From f46f5540f971d27918ba5baff12a6232ae077dca Mon Sep 17 00:00:00 2001 From: Charles Turner Date: Tue, 17 Dec 2024 13:39:15 +0800 Subject: [PATCH] Check for running event loop & start one if nothing found --- src/access_nri_intake/data/telemetry.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/access_nri_intake/data/telemetry.py b/src/access_nri_intake/data/telemetry.py index 8f0845f..8b554c0 100644 --- a/src/access_nri_intake/data/telemetry.py +++ b/src/access_nri_intake/data/telemetry.py @@ -29,6 +29,13 @@ async def send_telemetry(data): f"Request failed: {e}", category=RuntimeWarning, stacklevel=2 ) + # Check if there's an existing event loop, otherwise create a new one + try: + loop = asyncio.get_running_loop() + except RuntimeError: + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + # Schedule the telemetry data to be sent in the background asyncio.create_task(send_telemetry(telemetry_data)) return None