Skip to content

Commit 9f0b88e

Browse files
committed
Support running coroutines even when there is no existing event loop
1 parent 1aa72a6 commit 9f0b88e

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

fire/core.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,14 @@ def _CallAndUpdateTrace(component, args, component_trace, treatment='class',
678678

679679
# Call the function.
680680
if inspectutils.IsCoroutineFunction(fn):
681-
loop = asyncio.get_event_loop()
682-
component = loop.run_until_complete(fn(*varargs, **kwargs))
681+
try:
682+
loop = asyncio.get_running_loop()
683+
except RuntimeError:
684+
# No event loop running, create a new one
685+
component = asyncio.run(fn(*varargs, **kwargs))
686+
else:
687+
# Event loop is already running
688+
component = loop.run_until_complete(fn(*varargs, **kwargs))
683689
else:
684690
component = fn(*varargs, **kwargs)
685691

fire/inspectutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,6 @@ def GetClassAttrsDict(component):
345345

346346
def IsCoroutineFunction(fn):
347347
try:
348-
return asyncio.iscoroutinefunction(fn)
348+
return inspect.iscoroutinefunction(fn)
349349
except: # pylint: disable=bare-except
350350
return False

0 commit comments

Comments
 (0)