user18024637
user18024637

Reputation: 43

Call API in parallel loop

I am using a parallel loop for posting data to API from C#, having 1000+ objects in the list. The response is either success or failure only. I need to log failed record in the log file with appropriate ID. When I am sending the object to API, I keep the ID in local variable inside the loop and posting the data. When the response comes back, I am assuming that the response what I am getting for that ID and writing the log.

ParallelOptions po = new ParallelOptions
{
    MaxDegreeOfParallelism = 2
};
Parallel.ForEach(lstPost, po, obj=>
{
    string id= obj.Id;
    var response= API calls goes here......;
    if(response.Status== "ERROR")
    {
    //log the details
     Log.Write(ID : response.Status)
    }
}

My doubt is, there is any chance to get someone else's response to someone else's id?

Upvotes: 0

Views: 2001

Answers (1)

JonasH
JonasH

Reputation: 36341

It would be safe only if the API calls are thread safe. It would be a fairly safe assumption that they are, but I would always recommend checking the documentation before assuming anything in regards to thread safety.

The id and response inside the loop body are local variables, and thus not shared between any threads, and therefore perfectly safe. In general, the problem with thread safety is shared mutable state, if state is either non-shared or immutable it is perfectly safe.

You should also consider the thread safety of the logging function. Most log-libraries provide thread safe logging functions, but again, check the documentation to be safe. Directly writing to a file would likely require some synchronization, like a lock, to be safe.

However such a parallel loop will start two threads where each make a API-call. Most of the time making an API call will probably just be waiting for the result, and during this time the threads will just block, and this is a bit wasteful. Using Parallel.ForEachAsync or Task.WhenAll could allow your API calls to be made asynchronously, and therefore not use any thread at all while waiting for the response, saving some resources.

Upvotes: 2

Related Questions