[API Proposal]: Add Parallel.InvokeAsync

Open
#112,900 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp

Research direction

Start by reading the existing Parallel.Invoke and Parallel.ForEachAsync APIs and their implementation locations in the runtime repository. Compare the proposed overloads with the requested cancellation and failure behavior; done means the API design, semantics, and validation coverage are agreed.

Written by the indexing model from the issue text.

Description

api-suggestion area-System.Threading.Tasks
Background and motivation

Parallel.Invoke currently provides for concurrently executing a set of Actions, but does not provide for executing a set of async actions (Func<CancellationToken, ValueTask>). I have found a frequent need to concurrently execute a set of async actions, where if any action fails, all other actions are cancelled and the exception of the failing action is thrown. i.e., similar behavior is required that is provided by Parallel.ForEachAsync, but where ForEachAsync executes a single function for a collection of inputs, Parallel.InvokeAsync would execute a collection of async actions.

API Proposal
namespace System.Threading.Tasks

public static class Parallel
{
    public static Task InvokeAsync(params Func<CancellationToken, ValueTask>[] actions);

    public static Task InvokeAsync(CancellationToken cancellationToken, params Func<CancellationToken, ValueTask>[] actions);

    public static Task InvokeAsync(ParallelOptions parallelOptions, params Func<CancellationToken, ValueTask> actions);
}
API Usage
await Parallel.InvokeAsync(
  async ct => obj.Id = new(obj.Id.ExternalId, internalId: await idMapper.GetInternalId(obj.Id.ExternalId, ct)),
  async ct => obj.ParentId = new(obj.Id.ExternalId, internalId: await idMapper.GetInternalId(obj.ParentId.ExternalId, ct)),
  async ct => obj.OrderId = new(obj.Id.ExternalId, internalId: await idMapper.GetInternalId(obj.OrderId.ExternalId, ct)),
  async ct => obj.AssignmentId = new(obj.Id.ExternalId, internalId: await idMapper.GetInternalId(obj.AssignmentId.ExternalId, ct)));
Alternative Designs

No response

Risks

No response

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.