Skip to content

Commit

Permalink
.Net Processes - Facilitate Parallel Execution of LocalProcess (#9463)
Browse files Browse the repository at this point in the history
### Motivation and Context
<!-- 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.
-->

When using `async`/`await`, there is no guarantee that the method will
actually run asynchronously. The internal implementation is free to
return using a completely synchronous path.

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

Add `Task.Yield()` when starting `LocalProcess` to encourage the use of
a separate sychronization context from the current one.

-
https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.yield?view=net-8.0
- https://finchett.com/understanding-task-yield/
- https://www.webdevtutor.net/blog/c-sharp-taskyield-example

### 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 😄
  • Loading branch information
crickman authored Oct 30, 2024
1 parent 82f7cbd commit c371705
Showing 1 changed file with 1 addition and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ internal async Task StartAsync(Kernel? kernel = null, bool keepAlive = true)
internal async Task RunOnceAsync(KernelProcessEvent? processEvent, Kernel? kernel = null)
{
Verify.NotNull(processEvent);
await Task.Yield(); // Ensure that the process has an opportunity to run in a different synchronization context.
await this._externalEventChannel.Writer.WriteAsync(processEvent).ConfigureAwait(false);
await this.StartAsync(kernel, keepAlive: false).ConfigureAwait(false);
await this._processTask!.JoinAsync().ConfigureAwait(false);
Expand Down

0 comments on commit c371705

Please sign in to comment.