我应该关闭cURL吗? [英] Should I close cURL or not?

查看:586
本文介绍了我应该关闭cURL吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,使用 cURL 多次调用3个不同的API。每个API的结果都传递给下一个在嵌套循环中调用的API,因此cURL当前打开和关闭超过500次。

I have a function that calls 3 different APIs using cURL multiple times. Each API's result is passed to the next API called in nested loops, so cURL is currently opened and closed over 500 times.

我应该为整个功能打开cURL,还是可以在一个功能中打开和关闭这么多次?

Should I leave cURL open for the entire function or is it OK to open and close it so many times in one function?

推荐答案

重用同一句柄会提高性能。请参阅:重复使用相同的curl句柄。

There's a performance increase to reusing the same handle. See: Reusing the same curl handle. Big performance increase?

如果您不需要同步请求,请考虑使用curl_multi_ *函数(例如 curl_multi_init curl_multi_exec 等),这也提供了很大的性能提升。

If you don't need the requests to be synchronous, consider using the curl_multi_* functions (e.g. curl_multi_init, curl_multi_exec, etc.) which also provide a big performance boost.

UPDATE:

我尝试使用一个新的句柄为每个请求和使用相同的句柄与以下代码的弯曲curl:

I tried benching curl with using a new handle for each request and using the same handle with the following code:

ob_start(); //Trying to avoid setting as many curl options as possible
$start_time = microtime(true);
for ($i = 0; $i < 100; ++$i) {
    $rand = rand();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.google.com/?rand=" . $rand);
    curl_exec($ch);
    curl_close($ch);
}
$end_time = microtime(true);
ob_end_clean();
echo 'Curl without handle reuse: ' . ($end_time - $start_time) . '<br>';

ob_start(); //Trying to avoid setting as many curl options as possible
$start_time = microtime(true);
$ch = curl_init();
for ($i = 0; $i < 100; ++$i) {
    $rand = rand();
    curl_setopt($ch, CURLOPT_URL, "http://www.google.com/?rand=" . $rand);
    curl_exec($ch);
}
curl_close($ch);
$end_time = microtime(true);
ob_end_clean();
echo 'Curl with handle reuse: ' . ($end_time - $start_time) . '<br>';

并获得以下结果:

Curl without handle reuse: 8.5690529346466
Curl with handle reuse: 5.3703031539917


$ b b

因此,重复使用相同的句柄实际上提供了在连接到同一服务器多次时实质性的性能提高。我尝试连接到不同的服务器:

So reusing the same handle actually provides a substantial performance increase when connecting to the same server multiple times. I tried connecting to different servers:

$url_arr = array(
    'http://www.google.com/',
    'http://www.bing.com/',
    'http://www.yahoo.com/',
    'http://www.slashdot.org/',
    'http://www.stackoverflow.com/',
    'http://github.com/',
    'http://www.harvard.edu/',
    'http://www.gamefaqs.com/',
    'http://www.mangaupdates.com/',
    'http://www.cnn.com/'
);
ob_start(); //Trying to avoid setting as many curl options as possible
$start_time = microtime(true);
foreach ($url_arr as $url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_exec($ch);
    curl_close($ch);
}
$end_time = microtime(true);
ob_end_clean();
echo 'Curl without handle reuse: ' . ($end_time - $start_time) . '<br>';

ob_start(); //Trying to avoid setting as many curl options as possible
$start_time = microtime(true);
$ch = curl_init();
foreach ($url_arr as $url) {
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_exec($ch);
}
curl_close($ch);
$end_time = microtime(true);
ob_end_clean();
echo 'Curl with handle reuse: ' . ($end_time - $start_time) . '<br>';

得到以下结果:

Curl without handle reuse: 3.7672290802002
Curl with handle reuse: 3.0146431922913


$ b b

仍然有相当大的性能提升。

Still quite a substantial performance increase.

这篇关于我应该关闭cURL吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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