-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from santisq/37-invoke-parallel-not-correctly-…
…disposing-runspaces-take-2 Fixes `Invoke-Parallel` not disposing Runspaces, take 2
- Loading branch information
Showing
9 changed files
with
190 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Collections.Generic; | ||
using System.Management.Automation.Runspaces; | ||
using System.Threading.Tasks; | ||
|
||
namespace PSParallelPipeline; | ||
|
||
internal sealed class TaskManager | ||
{ | ||
private readonly List<Task<PSTask>> _tasks; | ||
|
||
private readonly Dictionary<int, Runspace> _assignedRunspaces; | ||
|
||
private readonly int _maxRunspaces; | ||
|
||
internal bool ShouldProcess { get => _tasks.Count == _maxRunspaces; } | ||
|
||
internal bool HasMoreTasks { get => _tasks.Count > 0; } | ||
|
||
internal TaskManager(int maxRunspaces) | ||
{ | ||
_maxRunspaces = maxRunspaces; | ||
_tasks = new List<Task<PSTask>>(maxRunspaces); | ||
_assignedRunspaces = new Dictionary<int, Runspace>(maxRunspaces); | ||
} | ||
|
||
internal void Enqueue(PSTask psTask) | ||
{ | ||
Task<PSTask> task = psTask.InvokeAsync(); | ||
_assignedRunspaces[task.Id] = psTask.Runspace; | ||
_tasks.Add(task); | ||
} | ||
|
||
internal Task<Task<PSTask>> WhenAny() => Task.WhenAny(_tasks); | ||
|
||
internal Runspace Dequeue(Task<PSTask> psTask) | ||
{ | ||
Runspace runspace = _assignedRunspaces[psTask.Id]; | ||
_assignedRunspaces.Remove(psTask.Id); | ||
_tasks.Remove(psTask); | ||
return runspace; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.