Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to do some async/await inside some .NET Parallel.ForEach() code?

Given the following code, is it OK to do async/await inside a Parallel.ForEach ?

eg.

Parallel.ForEach(names, name =>
{
    // Do some stuff...

    var foo = await GetStuffFrom3rdPartyAsync(name);

    // Do some more stuff, with the foo.
});

or is there some gotcha's that I need to be made aware of?

EDIT: No idea if this compiles, btw. Just Pseduo-code .. thinking out loud.

like image 524
Pure.Krome Avatar asked May 19 '14 04:05

Pure.Krome


1 Answers

From the name, I'm assuming that GetStuffFrom3rdPartyAsync is I/O-bound. The Parallel class is specifically for CPU-bound code.

In the asynchronous world, you can start multiple tasks and then (asynchronously) wait for them all to complete using Task.WhenAll. Since you're starting with a sequence, it's probably easiest to project each element to an asynchronous operation, and then await all of those operations:

await Task.WhenAll(names.Select(async name =>
{
  // Do some stuff...
  var foo = await GetStuffFrom3rdPartyAsync(name);
  // Do some more stuff, with the foo.
}));
like image 127
Stephen Cleary Avatar answered Nov 09 '22 01:11

Stephen Cleary