-
Notifications
You must be signed in to change notification settings - Fork 453
fix(batch): replace asyncio.get_event_loop() with future-compatible implementation for Python 3.12+ #7440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
fix(batch): replace asyncio.get_event_loop() with future-compatible implementation for Python 3.12+ #7440
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,7 +132,16 @@ async def async_process_closure(): | |
# whether we create an event loop (Lambda) or schedule it as usual (non-Lambda) | ||
coro = async_process_closure() | ||
if os.getenv(constants.LAMBDA_TASK_ROOT_ENV): | ||
loop = asyncio.get_event_loop() # NOTE: this might return an error starting in Python 3.12 in a few years | ||
# In Python 3.12+, asyncio.get_event_loop() raises a warning when called without a running loop | ||
# In Python 3.14+, it will raise an exception if no event loop is running | ||
# Use try/except to handle both the current behavior and future Python versions | ||
try: | ||
loop = asyncio.get_running_loop() | ||
except RuntimeError: | ||
# No running loop, create a new one for Lambda environment | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
Comment on lines
+138
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this code block is basically doing nothing with this try/except: trying to get a loop running or create a new one, but since Lambda doesn't have an active loop, it mostly falls into the "except" block, right? I haven't tested it yet, but I'm not sure if this is the correct implementation for it. |
||
|
||
task_instance = loop.create_task(coro) | ||
return loop.run_until_complete(task_instance) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic is incorrect.
asyncio.get_running_loop()
is used to get an already running event loop, but in a Lambda environment without an active loop, you should directly create a new one. The correct approach is to useasyncio.get_event_loop()
replacement pattern withasyncio.new_event_loop()
andasyncio.set_event_loop()
or useasyncio.run()
for the coroutine execution.Copilot uses AI. Check for mistakes.