From 6c8945f4d2d6f9d1d95701f65013fec36327040a Mon Sep 17 00:00:00 2001 From: JnyJny Date: Sat, 25 Nov 2023 10:01:21 -0600 Subject: [PATCH] Fix #289 Updated event loop property in busylight.lights.taskable.TaskableMixin As I understand it, when lights are activated in a thread other than the main thread there is not a default async event loop available and asyncio.get_event_loop raises a RuntimeError. I've modified the property TaskableMixin.event_loop to catch RuntimeError and create an event loop. --- busylight/lights/taskable.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/busylight/lights/taskable.py b/busylight/lights/taskable.py index 008e051..f2023fa 100644 --- a/busylight/lights/taskable.py +++ b/busylight/lights/taskable.py @@ -5,6 +5,8 @@ from typing import Any, Awaitable, Dict, Optional +from loguru import logger + class TaskableMixin: """This mixin class is designed to associate and manage @@ -18,7 +20,13 @@ def event_loop(self): return self._event_loop except AttributeError: pass - self._event_loop = asyncio.get_event_loop() + try: + self._event_loop = asyncio.get_event_loop() + except Exception as error: + logger.debug("get_event_loop raised {error}") + self._event_loop = asyncio.new_event_loop() + asyncio.set_event_loop(self._event_loop) + return self._event_loop @property