Files
semantic-kernel/python/semantic_kernel
Evan MattsonandEvan Mattson be89d6bc92 Python: Resolve hanging issue in Jupyter notebooks when re-running async functions (#4237)
### 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.

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### 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.

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [X] The code builds clean without any errors or warnings
- [X] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [X] All unit tests pass, and I have added new tests where possible
- [X] I didn't break anyone 😄

---------

Co-authored-by: Evan Mattson <[email protected]>
2023-12-13 18:31:53 +00:00
..