Activate throttle tasks?

I would like to know if asynchronous tasks need to be activated if the number of tasks to complete is large. Say you have 1000 URLs, you immediately run all the requests and wait for everyone:

var tasks = urlList.Select(url => downloadAsync(url));
await Task.WhenAll(tasks);

Or you load requests and process one batch after another:

foreach (var urlBatch in urlList.BatchEnumerable(BatchSize)){
    var tasks = urlBatch.Select(url => downloadAsync(url));
    await Task.WhenAll(tasks);
}

I thought that batch processing is not needed, because the first approach (starting all the requests at once) will create the tasks scheduled ThreadPool, so we need to ThreadPooldecide when to complete each task. However, I was told that in practice this only works if the tasks are computational tasks. When tasks are related to network requests, can the first approach cause the host to freeze? Why is this?

+4
source share
1 answer

You want to limit yourself to something in most cases. You always have some kind of condition when you work simultaneously with several operations. If they are connected to the CPU, then the tasks are stored in the queue ThreadPoolwaiting for the thread, and if it is asynchronized, then you have a state machine sitting on the heap.

Even asynchronous operations usually use some limited resource, be it throughput, ports, a remote database server processor, etc.

( , ). , SlimSemahpore , TPL:

var block = new ActionBlock<string>(
   url => downloadAsync(url),
   new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 10 });    

urlList.ForEach(url => block.Post(url));

block.Complete();
await block.Completion;
+7

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


All Articles