-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
[API Proposal]: Task.WhenEach #97355
Comments
Tagging subscribers to this area: @dotnet/area-system-threading-tasks Issue DetailsBackground and motivationThere are cases in which we have multiple tasks, and we have to process them as they complete. Currently, the only approach we have is either At least myself I have a real use case when I have to process a bunch of algorithms for backtesting, but each backtest can take from few milliseconds or even 15 full seconds depending on how big the algorithm is, so I don't like waiting for them all to finish to begin processing them but instead, process them as they finish. So I came up with this solution for myself and now I share it as I think it should be part of .net, I'm sure many people at some point may have had encoutered themselves with similar problems and made similar work-arounds. API Proposalnamespace System.Threading.Tasks;
public partial class Task
{
// example of implementation, actual implementation would probably be much more elaborated
// with actual internal logic that returns the tasks a they complete, kinda like WhenAny
// already does but as an asynchronous enumeration
// There could also be a ordered version if someone has the use-case of needing the tasks to be executed sequentially.
public static async IAsyncEnumerable<T> WhenEach(this IEnumerable<Task<T>> source)
{
var tasks = source.ToList();
while (tasks.Count > 0)
{
var task = Task.WhenAny(tasks);
tasks.Remove(task);
yield return task.Result;
}
}
} API Usagevar someExpensiveProcesses = Task.WhenEach(GetTasks());
await foreach (var process in someExpensiveProcesses)
{
DoStuff(process);
} Alternative DesignsNo response RisksNone that I know
|
Thanks. This is a duplicate of #61959. |
Background and motivation
There are cases in which we have multiple tasks, and we have to process them as they complete. Currently, the only approach we have is either
Task.WhenAll
, which requires all tasks to complete, andTask.WhenAny
that will return when any task complete but ignoring the rest unless tracked in other way.At least myself I have a real use case when I have to process a bunch of algorithms for backtesting, but each backtest can take from few milliseconds or even 15 full seconds depending on how big the algorithm is, so I don't like waiting for them all to finish to begin processing them but instead, process them as they finish.
So I came up with this solution for myself and now I share it as I think it should be part of .net, I'm sure many people at some point may have had encoutered themselves with similar problems and made similar work-arounds.
API Proposal
API Usage
Alternative Designs
No response
Risks
None that I know
The text was updated successfully, but these errors were encountered: