-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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]: Parallel.ForAsync #59019
Comments
Tagging subscribers to this area: @dotnet/area-system-threading-tasks Issue DetailsBackground and motivation#1946 recently As for the motivation I think the same arguments for API Proposalnamespace System.Threading.Tasks
{
public static class Parallel
{
public static Task ForAsync<T>(int fromInclusive, int toExclusive, Func<T, CancellationToken, ValueTask> body);
public static Task ForAsync<T>(int fromInclusive, int toExclusive, CancellationToken cancellationToken, Func<T, CancellationToken, ValueTask> body);
public static Task ForAsync<T>(int fromInclusive, int toExclusive, ParallelOptions parallelOptions, Func<T, CancellationToken, ValueTask> body);
public static Task ForAsync<T>(long fromInclusive, long toExclusive, Func<T, CancellationToken, ValueTask> body);
public static Task ForAsync<T>(long fromInclusive, long toExclusive, CancellationToken cancellationToken, Func<T, CancellationToken, ValueTask> body);
public static Task ForAsync<T>(long fromInclusive, long toExclusive, ParallelOptions parallelOptions, Func<T, CancellationToken, ValueTask> body);
}
} API UsageList<DuplicateItem> duplicates = new();
//duplicates.Add(...);
//...
await Parallel.ForAsync(0, duplicates.Count, new ParallelOptions { CancellationToken = cancellationTokenSource.Token}, async (i, token) => {
await duplicates.GenerateThumbnail();
for (int j = i + 1; j < duplicates.Count; j++) {
//...
}
}); RisksNo response
|
Any plan for this? |
Not currently. In the meantime, you can of course just use ForEachAsync with a range: Parallel.ForEachAsync(Enumerable.Range(0, count), async (i, cancellationToken) =>
{
...
}); |
Sort of. With Parallel.ForEachAsync, the primary motivation was being able to consume If we decide it's worthwhile to add Parallel.ForAsync, the proposed API set looks good to me. I've marked it as ready for review to have a broader discussion about whether we want ForAsync at all. |
Looks good as proposed namespace System.Threading.Tasks
{
public static class Parallel
{
public static Task ForAsync(int fromInclusive, int toExclusive, Func<int, CancellationToken, ValueTask> body);
public static Task ForAsync(int fromInclusive, int toExclusive, CancellationToken cancellationToken, Func<int, CancellationToken, ValueTask> body);
public static Task ForAsync(int fromInclusive, int toExclusive, ParallelOptions parallelOptions, Func<int, CancellationToken, ValueTask> body);
public static Task ForAsync(long fromInclusive, long toExclusive, Func<long, CancellationToken, ValueTask> body);
public static Task ForAsync(long fromInclusive, long toExclusive, CancellationToken cancellationToken, Func<long, CancellationToken, ValueTask> body);
public static Task ForAsync(long fromInclusive, long toExclusive, ParallelOptions parallelOptions, Func<long, CancellationToken, ValueTask> body);
}
} |
Temporarily moving back to api-ready-for-review to discuss collapsing the int/long overloads to be just based on |
We brought this back to discuss collapsing the int/long variants into a single generic representation, with a result of we do feel like one generic version is the better approach. The discussion says that IBinaryInteger is the correct restriction, so we're going with that. namespace System.Threading.Tasks
{
public static partial class Parallel
{
public static Task ForAsync<T>(T fromInclusive, T toExclusive, Func<T, CancellationToken, ValueTask> body) where T : notnull, IBinaryInteger<T>;
public static Task ForAsync<T>(T fromInclusive, T toExclusive, CancellationToken cancellationToken, Func<T, CancellationToken, ValueTask> body) where T : notnull, IBinaryInteger<T>;
public static Task ForAsync<T>(T fromInclusive, T toExclusive, ParallelOptions parallelOptions, Func<T, CancellationToken, ValueTask> body) where T : notnull, IBinaryInteger<T>;
}
} |
Background and motivation
#1946 recently
Parallel.ForEachAsync
has been approved and added to .NET6. However there still no async ofParallel.For
. Developers still have to use the same workarounds as forParallel.ForEach
on .NET5 and below to make it run asychronously. An async version ofParallel.For
has been mentioned two times in the linked issue, but was left out of discussion so I couldn't find a reason why it hasn't been made async yet. I simply assume that it wasn't taken into consideration because it was not explicit proposed.As for the motivation I think the same arguments for
Parallel.ForEachAsync
also applies toParallel.ForAsync
API Proposal
API Usage
Risks
No response
The text was updated successfully, but these errors were encountered: