.net core parallel processing

David Ribeiro 77 Reputation points
2020-09-28T20:23:22.783+00:00

Hi all,

I have an ASP.Net Core 3.1 API running in Azure, and I have a quite complex process that calls other APIs and some of these internal calls theorically could be made in parallel, gathering results later.

The purpose is if call to API 1 Takeshi 5 seconds and call to API 2 takes 2 seconds, theorically calling both would take 5 seconds and not 7. Output from both systems is diferent.

My question is which is the most suitable solution here?
Parallel.Foreach?
Task.WaitAll?
Other?

Thank you.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,207 questions
0 comments No comments
{count} votes

Accepted answer
  1. Krish G 2,326 Reputation points
    2020-09-29T09:48:43.367+00:00

    @Anonymous , there are multiple ways to achieve parallelism in .net with rich set of types and API in TPL. In your example of independent API calls where call to one API does not depend on the result of the other, you can do either of the below:

    • Spawning multiple calls to API (task) but not awaiting until needed like below: // Note: .ConfigureAwait(false) slightly improves the performance https://devblogs.microsoft.com/dotnet/configureawait-faq/
      var api1Task = CallApi1Async().ConfigureAwait(false); // Note I am not awaiting so that it does not block next until complete
      var api2Task = CallApi2Async().ConfigureAwait(false); // Note I am not awaiting so that it does not block next until complete
      var api3Task = CallApi3Async().ConfigureAwait(false); // Note I am not awaiting so that it does not block next until complete // now below I can do an await on each, this way, the overall time taken would be limited by the time taken by the longest running task and not the sum of them
      var api1Result = await api1Task;
      var api2Result = await api2Task;
      var api3Result = await api3Task;
    • Spawning multiple calls to API (task) and doing Task.WhenAll at the end like below: var tasks = new List<Task>();
      tasks.Add(CallApi1Async());
      tasks.Add(CallApi2Async());
      tasks.Add(CallApi3Async()); var results = await Task.WhenAll(tasks).ConfigureAwait(false);
    • For more sophisticated task scheduling, you can also consider using Dataflow blocks. This dataflow model promotes actor-based programming by providing in-process message passing for coarse-grained dataflow and pipelining tasks.

    For general .net questions like this, I would suggest you to leverage asp.net forum or stackoverflow to get broader community reach on such general programming topics.


0 additional answers

Sort by: Most helpful