Logo Questions Linux Laravel Mysql Ubuntu Git Menu

How to handle multiple request batch processing using Task in ASP.NET?

I have a list of selected contentIds and for each content id I need to call an api, get the response and then save the received response for each content in DB.

At a time a user can select any number of content ranging from 1-1000 and can pass on this to update the content db details after getting the response from api.

In this situation I end up creating multiple requests for each content.

I thought to go ahead with asp.net async Task operation and then wrote this following method.

The code I wrote currently creates one one task for each contentId and atlast I am waiting from all task to get the response.


public static Task<KeyValuePair<int, string>> GetXXXApiResponse(string url, int contentId)
    var client = new HttpClient();

    return client.GetAsync(url).ContinueWith(task =>
        var response = task.Result;
        var strTask = response.Content.ReadAsStringAsync();
        var strResponse = strTask.Result;

        return new KeyValuePair<int, string>(contentId, strResponse);

I am now thinking for each task I create it will create one thread and in turn with the limited no of worker thread this approach will end up taking all the threads, which I don't want to happen.

Can any one help/guide me how to handle this situation effectively i.e handling multiple api requests or kind of batch processing using async tasks etc?

FYI: I'm using .NET framework 4.5

like image 583
Maninder Avatar asked Mar 19 '23 18:03


1 Answers

A task is just a representation of an asynchronous operation that can be waited on and cancelled. Creating new tasks doesn't necessarily create new threads (it mostly doesn't). If you use async-await correctly you don't even have a thread during most of the asynchronous operation.

But, making many requests concurrently can still be problematic (e.g. burdening the content server too much). So you may still want to limit the amount of concurrent calls using SemaphoreSlim or TPL Dataflow:

private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(100);

public static async Task<KeyValuePair<int, string>> GetXXXApiResponse(string url, int contentId)
    await _semaphore.WaitAsync();
        var client = new HttpClient();
        var response = await client.GetAsync(url);
        var strResponse = await response.Content.ReadAsStringAsync();
        return new KeyValuePair<int, string>(contentId, strResponse);

To wait for these tasks to complete you should use Task.WhenAll to asynchronously wait instead of Task.WaitAll which blocks the calling thread:

await Task.WhenAll(apiResponses);
like image 117
i3arnon Avatar answered Mar 21 '23 14:03