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.
, , QueueFileToProcessAsync , , . TPL, 3 Async Producer/Consumer Queue .
, , , ServicePointManager.DefaultConnectionLimit, @Gerino.
ServicePointManager.DefaultConnectionLimit
, 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(); } } } }
IO, , - , , - .., ( ). , . , . - , , . -, , ( System.Net.ServicePointManager.DefaultConnectionLimit)
System.Net.ServicePointManager.DefaultConnectionLimit
, , , Parallel.ForEach. ( DegreesOfParallelism). 4 HT, 8 , DOP 8, . , ( CancellationToken).
Parallel.ForEach
DegreesOfParallelism
CancellationToken
Source: https://habr.com/ru/post/1674453/More articles:Unity 2D art blurry - pixelЕсли "ng-template" является веб-компонентом, добавьте "CUSTOM_ELEMENTS_SCHEMA" к этому компоненту, чтобы подавить это сообщение. ("[ERROR ->] - angularHow to implement custom runner in JUnit5 - javaPython: understanding a list of weird behavior - pythonSVG height not adjustable with aspect ratio - htmlHow to get data from a barcode scanner in Firemonkey / Delphi - androidNetfilterQueue set_payload not working - pythonHow can I use merge to have all time data? - mergeTestBed configureTestingModule определяет импорт, декларации, поставщики и трубы, общие для всех spec.ts, в одном модуле - шаблон - unit-testingUnswpive git status, difference, add (hang) - gitAll Articles