简体   繁体   中英

How many simultaneous (concurrent) connections are actually active during a many async request

My understanding is the point of Task is to abstract out threads, and that a new thread is not guaranteed per Task .

I'm debugging in VS2010, and I have something similar to this:

var request = WebRequest.Create(URL);

Task.Factory.FromAsync<WebResponse>(
    request.BeginGetResponse, 
    request.EndGetResponse).ContinueWith( 
    t => {  /* ... Stuff to do with response ... */ });

If I make X calls to this, eg start up X async web requests, how am I to calculate how many simultaneous (concurrent) connections are actually being made at any given time during execution? I assume that somehow it is opening only the max it can (in the case X is very high), and the other Tasks are blocked while waiting?

Any insight into this or how I can check with the debugger to determine how many active (open) connections are existent at a given point in execution would be great.

Basically, I'm wondering if it's handled for me, or if I have to take special consideration so that I do not appear to be attacking a server?

This won't really be specific to Task . The external connection is created as soon as you make your call to Task.Factory.FromAsync . The "task" that the Task is performing is simply waiting for the response to get back (not for it to be sent in the first place). Thus the call to BeginGetResponse will fail if your machine is unable to send any more requests, and the response will contain an error message if the server is rejecting your requests due to their belief that you are flooding them.

The only real place that Task comes into play here is the amount of time between when the response is actually received by the machine and when your continuation runs. If you are getting lots of responses, or otherwise have lots of work in the thread pool, it could take some time for it to get to your continuation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM