简体   繁体   English

php curl_multi_exec()花费的时间超过了CURLOPT_TIMEOUT_MS中设置的时间

[英]php curl_multi_exec() taking longer than time set in CURLOPT_TIMEOUT_MS

I am using curl_multi_exec() to get data from some APIs and using the following code : 我正在使用curl_multi_exec()从某些API获取数据,并使用以下代码:

        $startTime = microtime(true);
        $running = null; 
        do
        {
          curl_multi_exec ( $curlMultiHandleResource, $running );
          usleep(50000);
        }
        while ( $running > 0 );

        $runningTime = microtime(true)-$startTime;

and I have set CURLOPT_TIMEOUT_MS = 1800 and CURLOPT_CONNECTTIMEOUT_MS = 1800. On my windows machine $runningTime is always around 1.8 secs or lesser but on linux machine it sometimes comes out to be even more than 2.5 secs. 并且我已经设置CURLOPT_TIMEOUT_MS = 1800和CURLOPT_CONNECTTIMEOUT_MS =1800。在我的Windows机器上,$ runningTime总是大约1.8秒或更短,但是在linux机器上,它有时甚至超过2.5秒。 So I wanted to know why is this taking longer than CURLOPT_TIMEOUT_MS? 所以我想知道为什么花的时间比CURLOPT_TIMEOUT_MS要长?

EDIT : I narrowed down the problem somewhat. 编辑:我有点缩小了问题。 I echoed the time taken in the call to curl_multi_exec() and found that every time in the second iteration of while loop it is taking longer than usual. 我回呼了对curl_multi_exec()的调用所花费的时间,发现在while循环的第二次迭代中,每次都比平时花费更长的时间。 Usually it is taking 10^-3 to 10^-5 seconds but in second iteration it is taking 0.5 to 1.5 seconds. 通常,它花费10 ^ -3至10 ^ -5秒,但在第二次迭代中花费0.5至1.5秒。

After searching a lot I found that the AsynchDNS was not enabled on my Linux machine and hence it was sequentially resolving the domains in the urls and hence taking a lot of time. 经过大量搜索之后,我发现Linux计算机上未启用AsynchDNS ,因此它顺序解析了url中的域,因此花费了大量时间。 After updating my libcurl library to 7.21.7 and enabling the AsynchDNS it worked as expected. 将我的libcurl库更新为7.21.7并启用AsynchDNS后,它按预期工作。

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

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