怪异的卷曲+ php行为

时间:2014-03-16 08:20:03

标签: php curl libcurl

我的代码很简单。

function x($url, $request)
{
    static $curl = null;
    if (is_null($curl)) $curl = curl_init($url);
    $options = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array('Content-type: application/json'), CURLOPT_POST => true, CURLOPT_POSTFIELDS => $request);
    curl_setopt_array($curl, $options);
    $response = curl_exec($curl);
    echo curl_getinfo($curl)['total_time'].' ';
}
for ($i=0; $i<10000; $i++) x('http://server/', '<...post vars...>');

问题是大多数时候我在0.0001秒内从服务器得到响应,但有时它是1.0001或2.0001秒。 上面的代码可能会输出如下内容:

  

0.000632 0.00034的 2.001671 0.000526 0.000501 0.000914 0.007355 0.000769 0.001429 0.001249 0.000554 0.001623 0.000595 0.006834 0.000793 0.000436 0.000408 0.006953 0.000867 0.000593 0.000546 0.007408 0.000837 0.001208 0.000652 0.000947 0.000614 0.000641 0.000647 0.001288 0.000501 0.000582 0.000625 0.000288 0.000351 0.000557 0.000601 0.000259 0.000309 0.000541 0.000565 0.000582 0.000949 0.000403 0.000896 0.000487 0.000569 0.001233 1.002649 .0.001107

问题不在于服务器,因为如果我使用例如stream_context_create()+ fopen(),则没有这样的延迟。这似乎是卷曲本身的问题,但我无法弄清楚为什么有时会睡一两秒。

如果我每次没有差异时使用curl_close并重新初始化$ curl - 它有时会以相同的频率挂起。

提前感谢您的回复。

0 个答案:

没有答案