Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to cal API calls in parallel in .net Core, C#?

I would like to call my API in parallel x number of times so processing can be done quickly. I have three methods below that I have to call APIs in parallel. I am trying to understand which is the best way to perform this action.

Base Code

var client = new System.Net.Http.HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json");

client.BaseAddress = new Uri("https://jsonplaceholder.typicode.com");
var list = new List<int>();

var listResults = new List<string>();
for (int i = 1; i < 5; i++)
{
    list.Add(i);
}

1st Method using Parallel.ForEach

Parallel.ForEach(list,new ParallelOptions() { MaxDegreeOfParallelism = 3 }, index =>
{
    var response = client.GetAsync("posts/" + index).Result;

    var contents =  response.Content.ReadAsStringAsync().Result;
    listResults.Add(contents);
    Console.WriteLine(contents);
});

Console.WriteLine("After all parallel tasks are done with Parallel for each");

2nd Method with Tasks. I am not sure if this runs parallel. Let me know if it does

var loadPosts = new List<Task<string>>();
foreach(var post in list)
{
    var response = await client.GetAsync("posts/" + post);

    var contents = response.Content.ReadAsStringAsync();
    loadPosts.Add(contents);
    Console.WriteLine(contents.Result);
}

await Task.WhenAll(loadPosts);

Console.WriteLine("After all parallel tasks are done with Task When All");

3rd Method using Action Block - This is what I believe I should always do but I want to hear from community

var responses = new List<string>();

var block = new ActionBlock<int>(
    async x => {
        var response = await client.GetAsync("posts/" + x);
        var contents = await response.Content.ReadAsStringAsync();
        Console.WriteLine(contents);
        responses.Add(contents);                
    },
    new ExecutionDataflowBlockOptions
    {
        MaxDegreeOfParallelism = 6, // Parallelize on all cores
    });

for (int i = 1; i < 5; i++)
{
    block.Post(i);
}

block.Complete();
await block.Completion;

Console.WriteLine("After all parallel tasks are done with Action block");
like image 287
Learn AspNet Avatar asked Oct 11 '19 21:10

Learn AspNet


3 Answers

Approach number 2 is close. Here's a rule of thumb: I/O bound operations=> use Tasks/WhenAll (asynchrony), compute bound operations => use Parallelism. Http Requests are network I/O.

            foreach (var post in list)
            {
                async Task<string> func()
                {
                    var response = await client.GetAsync("posts/" + post);
                    return await response.Content.ReadAsStringAsync();
                }

                tasks.Add(func());
            }

            await Task.WhenAll(tasks);

            var postResponses = new List<string>();

            foreach (var t in tasks) {
                var postResponse = await t; //t.Result would be okay too.
                postResponses.Add(postResponse);
                Console.WriteLine(postResponse);
            }
like image 137
Yuli Bonner Avatar answered Oct 19 '22 01:10

Yuli Bonner


I made a little console app to test all the Methods at pinging API "https://jsonplaceholder.typicode.com/todos/{i}" 200 times. @MikeLimaSierra Method 1 or 3 were the fastest!

Method DegreeOfParallelism Time
Not Parallel n/a 8.4 sec
@LearnAspNet (OP) Method 1 2 5.494 sec
@LearnAspNet (OP) Method 1 30 1.235 sec
@LearnAspNet (OP) Method 3 2 4.750 sec
@LearnAspNet (OP) Method 3 30 1.795 sec
@jamespconnor Method n/a 21.5 sec
@YuliBonner Method n/a 21.4 sec
like image 28
Ted M Avatar answered Oct 19 '22 02:10

Ted M


I would use the following, it has no control of concurrency (it will dispatch all HTTP requests in parallel, unlike your 3rd Method) but it is a lot simpler - it only has a single await.

var client = new HttpClient();
var list = new[] { 1, 2, 3, 4, 5 };
var postTasks = list.Select(p => client.GetStringAsync("posts/" + p));
var posts = await Task.WhenAll(postTasks);
foreach (var postContent in posts)
{
    Console.WriteLine(postContent);
}
like image 31
jamespconnor Avatar answered Oct 19 '22 01:10

jamespconnor