Parallel execution of a loop using async

I'm writing a windows service and am looking for a way to execute multiple foreach loops in parallel, where each loop calls an asynchronous (TAP) method. I tried the following code first, which doesn't work because Parallel.ForEach and async / await are incompatible. Does anyone know if there is an alternative approach that can achieve this?

Parallel.ForEach(messagesByFromNumber, async messageGroup =>
{
    foreach (var message in messageGroup)
    {
        await message.SendAsync();
    }
});

      

For clarity, because of the way SendAsync () works, each copy of the foreach loop must be executed sequentially; in other words, the foreach loop cannot become parallel / concurrent.

+3


source to share


2 answers


Not necessary Parallel.Foreach

if your goal is to run them at the same time. Just go through all groups, create a task for each group that performs foreach

from SendAsync

, gets all tasks and await

all at once with Task.WhenAll

:



var tasks = messagesByFromNumber.Select(async messageGroup =>
{
    foreach (var message in messageGroup)
    {
        await message.SendAsync();
    }
});
await Task.WhenAll(tasks)

      

+4


source


You can make it cleaner with AsyncEnumerator NuGet Package :



using System.Collections.Async;

await messagesByFromNumber.ParallelForEachAsync(async messageGroup =>
{
    foreach (var message in messageGroup)
    {
        await message.SendAsync();
    }
}, maxDegreeOfParallelism: 10);

      

0


source







All Articles