什么时候最好检查完成异步请求卷曲? [英] When is it best to check asynchronous cURL requests for completion?

查看:163
本文介绍了什么时候最好检查完成异步请求卷曲?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多重卷曲请求是更好的以异步的方式进行,这是不相互等待,直到所有的previous请求已收到响应的请求。在许多情况下,其他的优化​​将开始处理接收到的响应不等待其他反应。然而,文档和官方的例子并不清楚当它是两种可能的的尽早检查完成的请求(这是使用典型的做法 curl_multi_info_read 功能)。

所以,当是检查完成的请求最早的点?或什么是这样的点的一组最佳?

这是从 curl_multi_exec 的页面(大写的意见是我的):

 < PHP//创建两个卷曲资源
$ CH1 = curl_init();
$ CH 2 = curl_init();//设置URL和其他适当的选项
curl_setopt($ CH1,CURLOPT_URLhttp://lxr.php.net/);
curl_setopt($ CH1,CURLOPT_HEADER,0);
curl_setopt($ CH2,CURLOPT_URLhttp://www.php.net/);
curl_setopt($ CH2,CURLOPT_HEADER,0);//创建多个卷曲手柄
$ MH = curl_multi_init();//添加两个手柄
curl_multi_add_handle($ MH,$ CH1);
curl_multi_add_handle($ MH,$ CH 2);$主动= NULL;
//执行手柄
做{
    $ MRC = curl_multi_exec($ MH,$活动);
}而($ MRC == CURLM_CALL_MULTI_PERFORM);
//应该请求所托运完成吗?
而($主动和放大器;&安培; $ MRC == CURLM_OK){
    如果(curl_multi_select($ MH)!= - 1){
        //应该请求所托运完成吗?
        做{
            $ MRC = curl_multi_exec($ MH,$活动);
        }而($ MRC == CURLM_CALL_MULTI_PERFORM);
        //应该请求所托运完成吗?
    }
    //应该请求所托运完成吗?
}
//应该请求所托运完成吗?//关闭句柄
curl_multi_remove_handle($ MH,$ CH1);
curl_multi_remove_handle($ MH,$ CH 2);
curl_multi_close($ MH);?>


解决方案

首先,以简化你的生活的 CURLM_CALL_MULTI_PERFORM回报code未在现代libcurls(在7.20.0或更高版本不使用)使用

然后,只要'积极'是大于零的有正在进行中至少有一个活动的转移,所以你可以检查的 curl_multi_info_read()如果你想。

或者你可以拨打 curl_multi_info_read() curl_multi_exec()的每一个电话,那是后给你!

Multiple cURL requests are better to be made in an asynchronous manner, that is without each of the requests waiting till all the previous requests have received responses. Another optimization in many cases would be starting to process a received response without waiting for other responses. However, the docs and official examples are not clear when it is both possible and as early as possible to check for completed requests (which is typically done using curl_multi_info_read function).

So when is the earliest point to check for completed requests? Or what is the optimal set of such points?

This is the example from the curl_multi_exec's page (comments in upper case are mine):

<?php

// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();

// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);

//create the multiple cURL handle
$mh = curl_multi_init();

//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$active = null;
//execute the handles
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
// SHOULD REQUESTS BE CHECKED FOR COMPLETION HERE?
while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        // SHOULD REQUESTS BE CHECKED FOR COMPLETION HERE?
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        // SHOULD REQUESTS BE CHECKED FOR COMPLETION HERE?
    }
    // SHOULD REQUESTS BE CHECKED FOR COMPLETION HERE?
}
// SHOULD REQUESTS BE CHECKED FOR COMPLETION HERE?

//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);

?>

解决方案

First, to simplify your life the CURLM_CALL_MULTI_PERFORM return code isn't used in modern libcurls (not used in 7.20.0 or later).

Then, as long as 'active' is larger than zero there are at least one active transfer in progress so you can wait with checking curl_multi_info_read() if you want.

Or you can call curl_multi_info_read() immediately after every call to curl_multi_exec(), that's up to you!

这篇关于什么时候最好检查完成异步请求卷曲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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