简体   繁体   English

C#HTTP请求上的CPU高负载

[英]c# high CPU load on HTTP requests

I have a bit of code that is responsible for doing http requests, there can be hundreds of threads per application that are active at a time. 我有一些代码负责执行http请求,每个应用程序一次可以有数百个线程处于活动状态。

Normally this works fine, however, when there are connection issues(at least I presume this to be the case) the code seems to use an excessive amount of CPU, upto 100%. 通常,这可以正常工作,但是,当出现连接问题时(至少我认为是这种情况),该代码似乎使用了过多的CPU,最多100%。

This is the piece of code in question: 这是有问题的代码:

 while (true)
            {
                try
                {
                    while (threadNr > newViewerCount || threadNr >= (proxies.Count))
                    {
                        Thread.Sleep(3000);
                    }

                    int sleepTimer = 4400 - (int)elapsed;
                    if (sleepTimer < 0)
                    {

                        sleepTimer = 1000;
                    }

                    Thread.Sleep(sleepTimer);

                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(rc.url);

                    request.Method = "GET";
                    request.UserAgent = agent;
                    request.Proxy = new WebProxy(proxy);
                    request.Timeout = 5000;

                    request.CookieContainer = ck;
                    request.Headers.Add("Accept-Language", "en,en-US;q=0.7,en;q=0.3");
                    request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                    request.Headers.Add("Accept-Encoding", "gzip, deflate");
                    request.UserAgent = userAgents[3];

                    HttpWebResponse webresponse;
                    using (webresponse = (HttpWebResponse)request.GetResponse())
                    {

                    }

                }
                catch (Exception e)
                {
                    int sl = 1500;
                    if (e.Message.Contains("timed"))
                    {
                        elapsed = 4399;
                        sl = 0;
                    }
                    Thread.Sleep(sl);
                    Console.WriteLine("Download exception:" + e.Message);

                    if (e.Message.Contains("404"))
                    {
                        Thread.Sleep(3000);
                        rc = getTokenUrl(proxy, ck); 
                    }

                    if (e.Message.Contains("forbidden"))
                    {
                        rc = getTokenUrl(proxy, ck);
                        Console.WriteLine("forbidden " + e.Message);
                    }
                }
            }

This application is running 24/7, and most of the time it's okay. 该应用程序正在全天候运行24/7,大多数时间都可以。 But at certain times it just starts using 100% cpu. 但是在某些时候,它只是开始使用100%cpu。 I believe this happens when the proxies are taking long to respond. 我相信当代理人花费很长时间做出回应时,就会发生这种情况。 Why could this possibly be the case, is there something in the code that I am overlooking that could cause the application to use 100% cpu? 为什么可能是这种情况,我忽略了代码中的某些内容,可能导致应用程序使用100%cpu?

You have a forever loop with while (true) , then there is an exception with message containing "timed", so s1 is set to zero and the loop is resumed. 您有一个while (true)的永久循环,然后消息包含“ timed”的异常,因此将s1设置为零,然后恢复循环。 Elapsed is 4399, you try to sleep for 1 millisecond, which might be actually zero. 经过的时间是4399,您尝试睡眠1毫秒,这实际上可能是零。 So of course it is using all available CPU power possible since these conditions just keep on happening and there are no sleeps anywhere. 因此,它当然会使用所有可能的CPU能力,因为这些情况一直在发生,而且任何地方都没有睡眠。

As was suggested, Tasks might be a better way to handle this and it's quite bad practice to have an eternal thread with no way of exiting or handling all error situations. 如建议的那样,任务可能是处理此问题的更好方法,并且拥有一个永生线程而无法退出或处理所有错误情况的方法是非常糟糕的做法。

Also I wouldn't use e.Message as a way to determine what happened. 另外,我不会使用e.Message作为确定发生了什么的方式。 What if the code is run on a machine that speaks French, for example? 例如,如果代码在说法语的机器上运行怎么办?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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