下面浅析通过提琴手行动code,我意识到,使用并行扩展我能够在最大2出站请求得到:
Analysing the code below in action through Fiddler, I realized that using Parallel Extensions I can get at maximum 2 outbound requests:
new string[]
{
"http://stackoverflow.com",
"http://superuser.com",
"http://serverfault.com",
"http://stackexchange.com"
}
.AsParallel()
.Select(a => HttpWebRequest.Create(a).GetResponse())
.ToArray()
;
我应该使用什么方法来最大限度地提高出站请求的数量?
What method should I use to maximize the number of outbound requests?
这code根据Wireshark的并行运行了我的机器上的所有6 HTTP请求:
This code runs all 6 HTTP requests in parallel on my machine according to Wireshark:
var urls = new string[]
{
"http://stackoverflow.com",
"http://superuser.com",
"http://serverfault.com",
"http://stackexchange.com",
"http://www.howtogeek.com",
"http://meta.stackoverflow.com"
};
var reqs = urls.Select<string, WebRequest>(HttpWebRequest.Create).ToArray();
var iars = reqs.Select(req => req.BeginGetResponse(null, null)).ToArray();
var rsps = reqs.Select((req, i) => req.EndGetResponse(iars[i])).ToArray();
基本上,它会创建一个的WebRequest
每个网址,电话 BeginGetResponse
每个然后调用 EndGetResponse
每个与的IAsyncResult
。
Basically it creates a WebRequest
for each URL, calls BeginGetResponse
on each and then calls EndGetResponse
for each with the IAsyncResult
.
该文档指出 BeginGetResponse
使用的托管线程池使HTTP请求。
The documentation states that BeginGetResponse
uses The Managed Thread Pool to make the HTTP request.