我有文本文件中的10万个URL的列表。现在我用等待/异步方法打开他们每一个 - 在速度非常好的时候(接近10,000个urls/min),但是当程序运行时,它在~10小时后降低到500个urls/min。当我重新启动程序并从乞讨运行时,情况是相同的 - 快速开始,然后慢慢。我正在使用Windows Server 2008 R2。测试我的代码在各种PC - 一些结果。你能告诉我问题在哪里吗?异步HttpClient请求减慢
int finishedUrls = 0;
IEnumerable<string> urls = File.ReadLines("urlslist.txt");
await urls.ForEachAsync(500, async url =>
{
Uri newUri;
if (!Uri.TryCreate(siteUrl, UriKind.Absolute, out newUri)) return false;
_uri = newUri;
var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(30));
string html = "";
using(var _httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(30),MaxResponseContentBufferSize = 300000 }) {
using(var _req = new HttpRequestMessage(HttpMethod.Get, _uri)){
using(var _response = await _httpClient.SendAsync(_req,HttpCompletionOption.ResponseContentRead,timeout.Token).ConfigureAwait(false)) {
if (_response != null &&
(_response.StatusCode == HttpStatusCode.OK || _response.StatusCode == HttpStatusCode.NotFound))
{
using (var cancel = timeout.Token.Register(_response.Dispose))
{
var rawResponse = await _response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
html = Encoding.UTF8.GetString(rawResponse);
}
}
}
}
}
Interlocked.Increment(ref finishedUrls);
});
http://blogs.msdn.com/b/pfxteam/archive/2012/03/05/10278165.aspx
这些URL是否都是同一个主机? –
您确定您的网络可以支持10000个请求/分钟吗?不知道有多大的回应,但你可能会遇到网络(或其他资源)的限制。 –
可能相关:http://stackoverflow.com/questions/10403944/does-httpwebrequests-limit-of-2-connections-per-host-apply-to-httpclient – davisoa