Skip to content

Commit

Permalink
Use Task.WhenEach
Browse files Browse the repository at this point in the history
  • Loading branch information
MattParkerDev committed Nov 13, 2024
1 parent 4e5452a commit c601ec4
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/ParallelPipelines/Host/Services/StepContainerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,21 @@ public async IAsyncEnumerable<StepContainer> GetStepContainersOrderedForExecutio
yield return container;
}

var inProgress = noDependencies;
var remaining = _stepContainers.Except(inProgress).ToList();
var remaining = _stepContainers.Except(noDependencies).ToList();

while (remaining.Count != 0)
var tasks = Task.WhenEach(_stepContainers.Select(s => s.CompletedTask).ToList());
await foreach (var task in tasks.WithCancellation(cancellationToken).ConfigureAwait(false))
{
var result = await await Task.WhenAny(inProgress.Select(s => s.CompletedTask).ToList()).WaitAsync(cancellationToken); // required, as otherwise the loop will not be cancellable
inProgress.Remove(result);
var result = await task;
foreach (var dependent in result.Dependents)
{
if (remaining.Contains(dependent) is false || dependent.Dependencies.Any(d => !d.HasCompleted))
if (remaining.Contains(dependent) is false || dependent.Dependencies.Any(d => d.HasCompleted is false))
{
continue;
}

yield return dependent;
inProgress.Add(dependent);
remaining.Remove(dependent);
yield return dependent;
}
}
}
Expand Down

0 comments on commit c601ec4

Please sign in to comment.