Can I wait for a set of 1000 tasks in a .NET application? Or should dosing be used?

I have a simple console application that checks some code.

My code has a list of 1000 numbers and puts each / int number in an Azure queue.

Now I am doing this asynchronously and it works fine. here is my code from my library:

var tasks = stagedFileIds.Select(stagedFileId => QueueFileToProcessAsync(stagedFileId));
await Task.WhenAll(tasks)
          .ConfigureAwait(false);

It works great.

But .. is that bad? Should I do it at 50 or 25 or something like that? But most importantly ... a package?

What is the "cost" of executing the above code?

Remember that this is a console application right now. I am going to move this to Azure Function at some point.

+4
source share
2 answers

, , QueueFileToProcessAsync , , . TPL, 3 Async Producer/Consumer Queue .

, , , ServicePointManager.DefaultConnectionLimit, @Gerino.

, TPL Dataflow, .NET Concurrent Collections:

// prototype code
static class TaskThrottlingExtension
{
    public static async Task ThrottleProcessingAsync<T>(this IEnumerable<T> inputs, int parallel, Func<T, Task> process)
    {
        var queues = new BlockingCollection<T>[parallel];
        var tasks = new Task[parallel];
        for (int i = 0; i < parallel; i++)
        {
            var queue = queues[i] = new BlockingCollection<T>(1);
            tasks[i] = Task.Run( async () =>
            {
                foreach (var input in queue.GetConsumingEnumerable())
                {
                    await process(input).ConfigureAwait(false);
                }
            });
        }

        try
        {
            foreach (var input in inputs)
            {
                BlockingCollection<T>.AddToAny(queues, input);
            }

            foreach (var queue in queues)
            {
                queue.CompleteAdding();
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);
        }
        finally
        {
            foreach (var queue in queues)
            {
                queue.Dispose();
            }
        }
    }
}
+2

IO, , - , , - .., ( ). , . , . - , , . -, , ( System.Net.ServicePointManager.DefaultConnectionLimit)

, , , Parallel.ForEach. ( DegreesOfParallelism). 4 HT, 8 , DOP 8, . , ( CancellationToken).

0

Source: https://habr.com/ru/post/1674453/


All Articles