Jump to content

How to parallelize a batch of HTTP requests using .NET 4.0's Parallel.ForEach

0
  JonUdell's Photo
Posted Dec 22 2010 08:13 AM

Introduction

In this week's companion article on the Radar blog, I discuss some of the ways that the elmcity service can scale. One way is to parallelize the gathering of iCalendar feeds. When I upgraded to .NET 4.0 it became trivial to do that. Here's a simplified version of the aggregator's central loop as it was originally:

Before


      foreach (var feedurl in feedurls)
      {
        var response = HttpUtils.FetchUrl(new Uri(feedurl));
        if (response.status != HttpStatusCode.OK)
          continue;
        ProcessFeed(response);
      }

And here's that same loop using the new .NET 4.0 constructs:

After


      Parallel.ForEach(source: feedurls, body: (feedurl, loop_state) =>
      {
        var response = HttpUtils.FetchUrl(new Uri(feedurl));
        if (response.status != HttpStatusCode.OK)
          loop_state.Break();
        ProcessFeed(response);
      } );

Results

Here are the results (in seconds):

1st run 2nd run 3rd run average
before 297 259 260 278.5
after 58 58 69 63.5

Conclusion

Tweak three lines of code, reap a better-than-4x speedup. What's not to like?



Tags:
0 Subscribe


0 Replies