Skip to content

Commit 7cd0a64

Browse files
Gabriel Ionescualindima
authored andcommitted
Create event loop when one does not exist
When executing a shell command from a thread, the event loop in the thread may be missing, so we should create one when that happens. Signed-off-by: Gabriel Ionescu <[email protected]>
1 parent a2a0bfd commit 7cd0a64

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

tests/framework/utils.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,13 @@ def run_cmd_list_async(cmd_list):
148148
:param cmd_list: list of commands to execute
149149
:return: None
150150
"""
151-
loop = asyncio.get_event_loop()
151+
loop = None
152+
try:
153+
loop = asyncio.get_event_loop()
154+
except RuntimeError:
155+
# Create event loop when one is not available
156+
loop = asyncio.new_event_loop()
157+
asyncio.set_event_loop(loop)
152158

153159
cmds = []
154160
# Create a list of partial functions to run
@@ -172,7 +178,13 @@ def run_cmd(cmd, ignore_return_code=False, no_shell=False):
172178
:param noshell: don't run the command in a sub-shell
173179
:returns: tuple of (return code, stdout, stderr)
174180
"""
175-
loop = asyncio.get_event_loop()
181+
loop = None
182+
try:
183+
loop = asyncio.get_event_loop()
184+
except RuntimeError:
185+
# Create event loop when one is not available
186+
loop = asyncio.new_event_loop()
187+
asyncio.set_event_loop(loop)
176188

177189
return loop.run_until_complete(
178190
run_cmd_async(cmd=cmd,

0 commit comments

Comments
 (0)