A .NET utility library for running async methods in parallel batches
$ dotnet add package CSRakowski.ParallelAsyncA .NET utility library for running async methods in parallel batches.
Available on NuGet:
and GitHub:
Example usage:
using CSRakowski.Parallel;
List<string> fileUrls = GetFileUrls();
var files = await ParallelAsync.ForEachAsync(fileUrls, (url) => {
return DownloadFileAsync(url);
}, maxBatchSize: 8, allowOutOfOrderProcessing: true);
As of version 1.1 a fluent syntax is also available:
using CSRakowski.Parallel.Extensions;
List<string> fileUrls = GetFileUrls();
var files = await fileUrls
.AsParallelAsync()
.WithMaxDegreeOfParallelism(8)
.WithOutOfOrderProcessing(false)
.ForEachAsync((url) => {
return DownloadFileAsync(url);
});
In version 1.6, support for Async Streams has been added.
This allows you to chain together multiple invocations by passing along an IAsyncEnumerable<T>, sort of like a pipeline:
using CSRakowski.Parallel;
List<string> fileUrls = GetFileUrls();
var fileDataStream = ParallelAsync.ForEachAsyncStream(fileUrls, (url) => {
return DownloadFileAsync(url);
}, maxBatchSize: 4, allowOutOfOrderProcessing: true);
var resultStream = ParallelAsync.ForEachAsyncStream(fileDataStream, (fileData) => {
return ParseFileAsync(fileData);
}, maxBatchSize: 4, allowOutOfOrderProcessing: true);
await foreach (var result in resultStream)
{
HandleResult(result);
}
CSRakowski.AsyncStreamsPreparations, which uses Microsoft.Bcl.AsyncInterfaces (correctly...).CSRakowski.AsyncStreamsPreparations, which uses Microsoft.Bcl.AsyncInterfaces.IAsyncEnumerable<T>.IAsyncEnumberable<T>T[], maxBatchSize is greater than 1 and allowOutOfOrder is falseT[] or IList<T> and maxBatchSize is set to 1allowOutOfOrder code paths.T on the IParallelAsyncEnumerable as covariantEventSource to expose some diagnostic information.EventSource).IReadOnlyCollection<T> to the ListHelper.ParallelAsync to prevent naming conflicts with the System.Threading.Tasks.Parallel.CSRakowski.Parallel to prevent ambiguous name conflicts between the class and the namespace.