多卷曲 (curl_multi_select) 的最新正确用法? [英] UP-TO-DATE correct usage of multi cURL (curl_multi_select )?

查看:102
本文介绍了多卷曲 (curl_multi_select) 的最新正确用法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看 php.net 示例后和贡献者代码,我发现有不同的方法,但是其中一些方法在测试后不起作用或 已弃用.在互联网上不同的文章建议了不同的方法:

After looking at php.net examples and contributor codes, I found that there are different approaches, however some of them either doesn't work after testing or are deprecated. Over internet different articles suggest different approaches:

do {
    curl_multi_exec($mh,$active); 
}  
while ($active > 0);

其他示例/程序员使用高级"方式:

other examples/programmers used "advanced" way:

do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) == -1) {
        usleep(1000);
    }
    else {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

谁能告诉我 curl_multi_exec 的最新和最佳用法是什么?(顺便说一句,就我个人而言,我发现doenstele进行任何表演)

Can anyone tell, which is the up-to-date and best usage of curl_multi_exec ? (btw, personally me, i've found that usleep doenst make any performance )

推荐答案

usleep() 调用不会提高性能".在函数不等待任何东西而是立即返回的情况下,它可以避免繁忙循环.在传输开始的名称解析器阶段,对于(一些较旧的)libcurl 版本来说,这尤其可能发生.(当 CURL/PHP 不再有那样的行为时,这种预防措施可能会在将来被移除.)

The usleep() call doesn't "improve performance". It is there to avoid a busy-loop for the case where the function doesn't wait on anything but return instantly. That can happen in particular for (some older) libcurl versions during the name resolver phase in the beginning of a transfer. (That precaution can be probably removed in the future when CURL/PHP won't behave like that anymore.)

但是你肯定可以跳过对 CURLM_CALL_MULTI_PERFORM 的检查,因为 libcurl 已经很多年没有返回了.所以,这将使它只是

But you can for sure skip the checks for CURLM_CALL_MULTI_PERFORM since libcurl hasn't returned that since very many years. So, that would make it just

$active = 1;
$mrc = CURLM_OK;
while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) == -1) {
        usleep(1000);
    }
    else {
       $mrc = curl_multi_exec($mh, $active);
    }
}

这篇关于多卷曲 (curl_multi_select) 的最新正确用法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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