尝试并行运行多个HTTP请求,而是由Windows的限制(注册表) [英] Trying to run multiple HTTP requests in parallel, but being limited by Windows (registry)

查看:296
本文介绍了尝试并行运行多个HTTP请求,而是由Windows的限制(注册表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序(的WinForms C#.NET 4.0),我从第三方通过一个简单的HTTP请求访问查找功能。我叫URL带参数,作为回报,我得到一个小串与查找的结果。够简单了。

I'm developing an application (winforms C# .NET 4.0) where I access a lookup functionality from a 3rd party through a simple HTTP request. I call an url with a parameter, and in return I get a small string with the result of the lookup. Simple enough.

挑战然而,我必须做大量的这些查找的(几十万),我想限制所需要的时间。因此,我想并行运行的要求(比如10-20)。我用一个线程池来做到这一点,我的code短版本是这样的:

The challenge is however, that I have to do lots of these lookups (a couple of thousands), and I would like to limit the time needed. Therefore I would like to run requests in parallel (say 10-20). I use a ThreadPool to do this, and the short version of my code looks like this:

public void startAsyncLookup(Action<LookupResult> returnLookupResult)
{
    this.returnLookupResult = returnLookupResult;

    foreach (string number in numbersToLookup)
    {
        ThreadPool.QueueUserWorkItem(lookupNumber, number);
    }
}

public void lookupNumber(Object threadContext)
{
    string numberToLookup = (string)threadContext;
    string url = @"http://some.url.com/?number=" + numberToLookup;
    WebClient webClient = new WebClient();
    Stream responseData = webClient.OpenRead(url);
    LookupResult lookupResult = parseLookupResult(responseData);

    returnLookupResult(lookupResult);
}

我填写 numbersToLookup (A 列表&LT;弦乐&GT; )的另一个地方,通话 startAsyncLookup 并为它提供一个回调函数 returnLooku presult 返回每个结果。这工作,但我发现,我没有得到我想要的吞吐量。

I fill up numbersToLookup (a List<String>) from another place, call startAsyncLookup and provide it with a call-back function returnLookupResult to return each result. This works, but I found that I'm not getting the throughput I want.

起初,我想这可能是对他们的最终一个贫穷的系统第三方,但我试图通过在同一时间运行两个不同的机器同样code排除这一点。每两个了,只要一家独做,所以我可以排除的。

Initially I thought it might be the 3rd party having a poor system on their end, but I excluded this by trying to run the same code from two different machines at the same time. Each of the two took as long as one did alone, so I could rule out that one.

有一个同事,然后放倒我,这可能是Windows中的一个限制。我用Google搜索了一下,发现除其他这个帖子说,由默认的Windows限制同时数请求发往同一Web服务器4 HTTP 1.0和2 HTTP 1.1(支持HTTP 1.1这实际上是根据规范(RFC2068))。

A colleague then tipped me that this might be a limitation in Windows. I googled a bit, and found amongst others this post saying that by default Windows limits the number of simultaneous request to the same web server to 4 for HTTP 1.0 and to 2 for HTTP 1.1 (for HTTP 1.1 this is actually according to the specification (RFC2068)).

以上提及的相同交还提供了一种方法来增加这些限制。通过添加两个注册表值 [HKEY_CURRENT_USER \\软件\\微软\\的Windows \\ CurrentVersion \\ Internet设置] (MaxConnectionsPerServer的和为MaxConnectionsPer1_0Server),我能控制这个自己。

The same post referred to above also provided a way to increase these limits. By adding two registry values to [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings] (MaxConnectionsPerServer and MaxConnectionsPer1_0Server), I could control this myself.

所以,我尝试这样做(坐既20),重新启动我的电脑,并试图再次运行我的程序。不过不幸的是,它似乎没有任何帮助。我也在不停的资源监视器眼(见截图),同时运行我的批处理查找,和我注意到我的应用程序(一个与标题涂黑)仍然只使用两个TCP连接了。

So, I tried this (sat both to 20), restarted my computer, and tried to run my program again. Sadly though, it didn't seem to help any. I also kept an eye on the Resource Monitor (see screen shot) while running my batch lookup, and I noticed that my application (the one with the title blacked out) still only was using two TCP connections.

所以,问题是,为什么没有这方面的工作?是我挂使用错误的注册表值后?这也许是不可能的黑客在Windows不再(我在Windows 7中)?

So, the question is, why isn't this working? Is the post I linked to using the wrong registry values? Is this perhaps not possible to "hack" in Windows any longer (I'm on Windows 7)?

任何想法将是非常美联社preciated:)

Any ideas would be highly appreciated :)

和以防万一任何人都应该知道,我也为MaxThreads不同的设置试过线程池(everyting从10到100),这似乎并没有影响到我的吞吐量所有,所以这个问题不应该是有两种。

And just in case anyone should wonder, I have also tried with different settings for MaxThreads on ThreadPool (everyting from 10 to 100), and this didn't seem to affect my throughput at all, so the problem shouldn't be there either.

推荐答案

这件事的的的ServicePoint 。它提供了连接管理HTTP连接。
用的ServicePoint对象允许的并发连接的默认最大数是2。
所以,如果你需要增加它,你可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit.aspx\">ServicePointManager.DefaultConnectionLimit属性。只是检查MSDN中的链接,在那里,你可以看到一个样本。并设置您需要的值。

It is matter of ServicePoint. Which provides connection management for HTTP connections. The default maximum number of concurrent connections allowed by a ServicePoint object is 2. So if you need to increase it you can use ServicePointManager.DefaultConnectionLimit property. Just check the link in MSDN there you can see a sample. And set the value you need.

这篇关于尝试并行运行多个HTTP请求,而是由Windows的限制(注册表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆