jamauss
jamauss

Reputation: 1043

Parallel.ForEach in C# - something I"m missing?

I'm trying to figure out why what I am trying to build (using TPL) is not working as expected.

Here's the scenario: I have a bunch of service request classes that go through a central service processor, and return service result objects. Sometimes I need to execute multiple (I'd say between 2 and 5) of these requests for a single "call". So I have something like this:

ServiceRequestType1 Request1 = new ServiceRequestType1();
ServiceRequestType2 Request2 = new ServiceRequestType2();
ServiceRequestType3 Request3 = new ServiceRequestType3();
ServiceRequestType4 Request4 = new ServiceRequestType4();

ServiceProcessor sp = new ServiceProcessor();

ServiceResultType1 = sp.ProcessType1(Request1);
ServiceResultType2 = sp.ProcessType2(Request2);
ServiceResultType3 = sp.ProcessType3(Request3);
ServiceResultType4 = sp.ProcessType4(Request4);

// do stuff with the ServiceResult objects...

These 4 requests, when running synchronously on my local machine - all process very quickly (usually in under 10 ms each) even when they are accessing a database for their data. I'm happy with that performance. Over time though, as the size of the database grows and performance slows, I wanted to see if I could get multiple requests to run in roughly the same amount of time in parallel using Parallel.ForEach().

So I setup something like so:

ConcurrentBag<ServiceResultBase> results = new ConcurrentBag<ServiceResultBase>();

Parallel.ForEach<IServiceRequest>(request.Requests, serviceRequest =>
{
   ServiceResultBase serviceResult = ExecuteServiceRequest(serviceRequest);
   results.Add(serviceResult);
});

As I've been testing now, the problem is that I get very inconsistent results. Sometimes all requests run great together and all 4 execute in < 10 ms total. Other times some requests run in < 10 ms but others take over 3000 ms, and then sometimes one or more will take over 30,000 ms to run. Sometimes one or more requests won't complete at all. I'm trying to debug and get to the root cause of this but so far it's been difficult to tell where the issue is and why Parallel.ForEach is behaving this way for me.

Are there any gotchas or options I should be aware of when using .ForEach() ? I've read some other posts on here referencing TPL but nothing really the same as what I'm experiencing.

Upvotes: 1

Views: 403

Answers (1)

MeTitus
MeTitus

Reputation: 3428

It surely has nothing to do with TPL, the problem lies with either the library you're using to access the database, or with the database itself, some kind or concurrency problems related to table/row lock. Check the queries' execution with SQL Profiler and go from there.

Upvotes: 1

Related Questions