【问题标题】:PHP Multi Curl is Running Very Slow On High Traffic Server ClusterPHP Multi Curl 在高流量服务器集群上运行非常缓慢
【发布时间】:2013-10-19 15:10:31
【问题描述】:

在使用多卷曲时遇到速度问题。我正在使用 multi curl 从各种 url 中获取 XML,所有响应时间都低于 300 毫秒。但是我的多卷曲功能需要 1 秒以上的时间来获取这些 URL(仅大约 10-15 个 URL)。以下是我正在使用的代码:

function multiRequest($data, $options = array()) {

    // array of curl handles
    $curly = array();
    // data to be returned
    $result = array();

    // multi handle
    $mh = curl_multi_init();

    // loop through $data and create curl handles
    // then add them to the multi-handle
    foreach ($data as $id => $d) {

    $curly[$id] = curl_init();

    $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
    curl_setopt($curly[$id], CURLOPT_URL,            $url);
    curl_setopt($curly[$id], CURLOPT_HEADER,         0);
    curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curly[$id], CURLOPT_NOSIGNAL, 1);
    curl_setopt($curly[$id], CURLOPT_TIMEOUT_MS, 750);


    curl_multi_add_handle($mh, $curly[$id]);
    }

    // execute the handles
    do {
        curl_multi_select($mh, .01);
        $status = curl_multi_exec($mh, $running);
    } while ($status === CURLM_CALL_MULTI_PERFORM || $running);


    // get content and remove handles
    foreach($curly as $id => $c) {
        if(curl_errno($c) == 0)
            $result[$id] = curl_multi_getcontent($c);
        curl_multi_remove_handle($mh, $c);
    }

    // all done
    curl_multi_close($mh);

    return $result;
}

我应该做些什么来加快速度?如果请求需要超过 500 毫秒才能完成,我的客户会丢弃该请求,所以我想让它运行,只要最长的请求需要。我将超时设置为 750 毫秒,因为即使请求时间小于 300 毫秒,如果低于 750 毫秒,我的函数也不会返回任何结果。

【问题讨论】:

    标签: php curl curl-multi


    【解决方案1】:

    低于此然后 curl_setopt($curly[$id], CURLOPT_TIMEOUT_MS, 750);至 500 毫秒

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-20
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多