Skip to content
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

Closed
JustNrik opened this issue Jan 23, 2024 · 2 comments
Closed

[API Proposal]: Task.WhenEach #97355

JustNrik opened this issue Jan 23, 2024 · 2 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Threading.Tasks

Comments

@JustNrik
Copy link

JustNrik commented Jan 23, 2024

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, and Task.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

namespace 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 = await Task.WhenAny(tasks);
            tasks.Remove(task);
            yield return task.Result;
        }
    }
}

API Usage

var someExpensiveProcesses = Task.WhenEach(GetTasks());

await foreach (var process in someExpensiveProcesses)
{
    DoStuff(process);
}

Alternative Designs

No response

Risks

None that I know

@JustNrik JustNrik added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jan 23, 2024
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jan 23, 2024
@ghost
Copy link

ghost commented Jan 23, 2024

Tagging subscribers to this area: @dotnet/area-system-threading-tasks
See info in area-owners.md if you want to be subscribed.

Issue Details

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, and Task.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

namespace 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 Usage

var someExpensiveProcesses = Task.WhenEach(GetTasks());

await foreach (var process in someExpensiveProcesses)
{
    DoStuff(process);
}

Alternative Designs

No response

Risks

None that I know

Author: JustNrik
Assignees: -
Labels:

api-suggestion, area-System.Threading.Tasks

Milestone: -

@stephentoub
Copy link
Member

Thanks. This is a duplicate of #61959.

@stephentoub stephentoub closed this as not planned Won't fix, can't repro, duplicate, stale Jan 23, 2024
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jan 23, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Feb 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Threading.Tasks
Projects
None yet
Development

No branches or pull requests

2 participants