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

Clarify differences between ForEachAsyncExtensions and ParallelForEachExtensions #21

Closed
MNF opened this issue Dec 16, 2017 · 1 comment

Comments

@MNF
Copy link

MNF commented Dec 16, 2017

The thread https://stackoverflow.com/questions/11564506/nesting-await-in-parallel-foreach/11565531#11565531 has an answer suggested to use Task.WhenAll to run multiple (degreeOfParallel) asynchronous tasks in parallel, not waiting until previous task is completed.

public static Task ForEachAsync<T>(
      this IEnumerable<T> source, int dop, Func<T, Task> body) 
{ 
    return Task.WhenAll( 
        from partition in Partitioner.Create(source).GetPartitions(dop) 
        select Task.Run(async delegate { 
            using (partition) 
                while (partition.MoveNext()) 
                    await body(partition.Current).ContinueWith(t => 
                          {
                              //observe exceptions
                          });
})); 
}

Your library has a few ForEachAsyncExtensions and ParallelForEachExtensions, that seems to do similar things.
It will be good if you add clarification in readme.md or wiki what is the difference between ForEachAsyncExtensions and ParallelForEachExtensions. It also will be good to clarify how ParallelForEachExtensions are different from MS Parallel.ForEach.

Also I wish to know your opinion, is mentioned above ForEachAsync good enough, or there are some limitations of this approach that some methods of your library addresses

@kind-serge
Copy link
Member

Hi, I added more info to the Readme - see pt.9 to describe the difference. The MS version of Parallel.ForEach does not work with async-enumerable collections and async delegates, what leads to thread-blocking calls if you try to use it - that's what this library solves, avoids such blocking IO calls with IAsyncEnumerable and async delegates.
Hope that answers your questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants