Skip to content

Commit 15eca05

Browse files
committed
Make get_asyncio_loop safe to use repeatedly
1 parent 2dac593 commit 15eca05

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

megatron/core/utils.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,11 +2001,12 @@ def unwrap_model(model, module_instances=None):
20012001
return unwrapped_model
20022002

20032003

2004-
def get_asyncio_loop():
2004+
def get_asyncio_loop(loop: asyncio.AbstractEventLoop | None = None) -> asyncio.AbstractEventLoop:
20052005
"""Creates an asyncio loop if necessary and then returns the current asyncio loop."""
2006-
try:
2007-
loop = asyncio.get_running_loop()
2008-
except RuntimeError as e:
2009-
loop = asyncio.new_event_loop()
2010-
asyncio.set_event_loop(loop)
2006+
if loop is None:
2007+
try:
2008+
loop = asyncio.get_running_loop()
2009+
except RuntimeError as e:
2010+
loop = asyncio.new_event_loop()
2011+
asyncio.set_event_loop(loop)
20112012
return loop

0 commit comments

Comments
 (0)