Skip to content

Commit 9bb153d

Browse files
committed
Use get_running_loop() instead of str cmp to check for a running event loop
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 82e131b commit 9bb153d

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

src/frequenz/sdk/_internal/_asyncio.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,10 @@ async def run_forever(
4646
while True:
4747
try:
4848
await async_callable()
49-
except RuntimeError as exc:
50-
if "no running event loop" in str(exc):
51-
_logger.exception(
52-
"Something went wrong, no running event loop, skipping execution of %s",
53-
async_callable.__name__,
54-
)
49+
except RuntimeError:
50+
try:
51+
asyncio.get_running_loop()
52+
except RuntimeError:
5553
return
5654
except Exception: # pylint: disable=broad-except
5755
if not asyncio.get_event_loop().is_running():

src/frequenz/sdk/actor/_actor.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,16 @@ async def _run_loop(self) -> None:
105105
_logger.info("Actor %s: Cancelled.", self)
106106
raise
107107
except Exception as exc: # pylint: disable=broad-except
108-
if isinstance(exc, RuntimeError) and "no running event loop" in str(
109-
exc
110-
):
111-
_logger.exception(
112-
"Something went wrong, no running event loop,"
113-
" not trying to restart %s again.",
114-
self,
115-
)
116-
break
108+
if isinstance(exc, RuntimeError):
109+
try:
110+
asyncio.get_running_loop()
111+
except RuntimeError:
112+
_logger.exception(
113+
"Something went wrong, no running event loop,"
114+
" not trying to restart %s again.",
115+
self,
116+
)
117+
break
117118
if self._is_cancelled:
118119
# If actor was cancelled, but any tasks have failed with an exception
119120
# other than asyncio.CancelledError, those exceptions are combined

0 commit comments

Comments
 (0)