Should we use MaxDegreeOfParallelism option when use ParallelForEachAsync

Ronaldo C 21 Reputation points
2021-05-26T18:17:27.46+00:00

I have a list of objects and for each item on the list I have to consume an API. I used ParallelForEachAsync, but I was unsure whether to change the maxDegreeOfParallelism setting.

await listObject.ParallelForEachAsync(
async item =>
{

                await CallAPI();


            }, maxDegreeOfParallelism: Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 0.75) * 2.0)));
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,542 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-05-27T02:29:42.037+00:00

    Unless you have a special reason, you don’t have to explicitly set it

    Some explanations are provided in the documentation:

    By default, For and ForEach will utilize however many threads the underlying scheduler provides, so changing MaxDegreeOfParallelism from the default only limits how many concurrent tasks will be used.

    Generally, you do not need to modify this setting. However, you may choose to set it explicitly in advanced usage scenarios such as these:

    When you know that a particular algorithm you're using won't scale beyond a certain number of cores. You can set the property to avoid wasting cycles on additional cores.

    When you're running multiple algorithms concurrently and want to manually define how much of the system each algorithm can utilize. You can set a MaxDegreeOfParallelism value for each.

    When the thread pool's heuristics is unable to determine the right number of threads to use and could end up injecting too many threads. For example, in long-running loop body iterations, the thread pool might not be able to tell the difference between reasonable progress or livelock or deadlock, and might not be able to reclaim threads that were added to improve performance. In this case, you can set the property to ensure that you don't use more than a reasonable number of threads.

    It limits the upper limit of parallel operations, which may reduce efficiency at certain times.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.