The task is as follows: upon arrival of the track (there may be thousands of them), it is necessary to request data from the scrobbler, process the response (in the example not shown for the sake of clarity), and write to the file. I decided to use Tpl.Dataflow and this is what happened:
static void Main() { HttpClient hc = new HttpClient(); StreamWriter sw = new StreamWriter(@"C:\res.txt"); // первый вариант TransformBlock<string, string> tb = new TransformBlock<string, string>(item => hc.GetStringAsync(item), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 4, BoundedCapacity = 200 }); //второй вариант //TransformBlock<string, string> tb = new TransformBlock<string, string>(item => new HttpClient().GetStringAsync(item), new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 4, BoundedCapacity = 200 }); ActionBlock<string> ab = new ActionBlock<string>(item => { sw.WriteLine(item); sw.WriteLine("________________________________________________"); }, new ExecutionDataflowBlockOptions { BoundedCapacity = 200 }); tb.LinkTo(ab); tb.Completion.ContinueWith(item => ab.Complete()); ab.Completion.ContinueWith(item => sw.Dispose()); Stopwatch swa = new Stopwatch(); swa.Start(); foreach (var item in urls) { tb.Post(item); } tb.Complete(); ab.Completion.Wait(); Console.WriteLine(swa.ElapsedMilliseconds); } As you probably already noticed, there are two branches of the solution:
In the first variant, I use the same
HttpClientobject, but it can simultaneously send only two requests (by the way, why is that?),HttpClientwhy the whole process takes quite a while
The second option is already faster, and much faster, if you parallelize not on 4, but on 20, for example, but also not without drawbacks: the creation of each
HttpClientobject entails setting up a connection (handshake), which is logical. But as I understand it, this is an extra overhead that can be avoided. What the question is about for me:
is it possible to somehow bind one HttpClient object to a task so that when it executes one request, the second request occurs through the same HttpClient object and the connection is not established. That is, I want the HttpClient objects HttpClient be as much as MaxDegreeOfParallelism and each task to use an HttpClient object that is not occupied by other tasks at the moment. Well, or another effective solution
Thank you in advance