-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Python: Resolve hanging issue in Jupyter notebooks when re-running async functions #4237
Python: Resolve hanging issue in Jupyter notebooks when re-running async functions #4237
Conversation
…unning async functions.
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.
Looks good, thanks for fixing this!
I think this fix has caused a new problem. When trying to run the semantic functions with input parameters from this notebook [https://github.com/microsoft/semantic-kernel/blob/main/python/README.md], my event loop hangs now: In addition, when trying to run this code as a standalone python file, I get RuntimeError: There is no current event loop in thread 'MainThread'. I'm trying to find the exact problem and fix associated with this but reporting here so the community can also look into it. |
I am seeing the same issue as @BeeShall . |
Error I get is:
It succeeds on the first call, but then fails on the second one after |
@moonbox3 Yes that's the one. keeps hanging in Jupyter notebook in a thread lock situation and I get that error when I try to run standalone. @NatanMish yep that's exactly how I see it too. |
@BeeShall, thanks for confirming. And @NatanMish, thank you for the code. Appreciate you pointing this out. This is fixed in #4485. |
Motivation and Context
Resolves #4137. The issue was that if you try to re-run the sample joke in the 00-getting-started notebook, the subsequent call to the joke hangs.
Description
There were issues when re-running asynchronous functions (
_invoke_semantic_async
and_invoke_native_async
) due to improper handling of asyncio event loops in a multi-threaded env. Specifically, the code had an issue when an event loop was already running, leading to hanging or blocking behavior on subsequent runs.The change replaced the old code Python's ThreadPoolExecutor, which simplifies thread management. It handles the creation, execution, and cleanup of threads more efficiently and safely.
The new
run_async_in_executor
method ensures that each async function runs in its own new event loop, avoiding conflicts with the main thread's event loop or other threads.Contribution Checklist