理解 php curl_multi_exec
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15559157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
understanding php curl_multi_exec
提问by glennv
I'm trying to understand curl_multi_exec. I've copied a piece of the manual example here. So I'm wondering, how does it work? The first loop sends the http request I guess? But it then it is followed by a loop inside a loop using functions with seemingly undocumented flags..
我正在尝试了解 curl_multi_exec。我在这里复制了一段手动示例。所以我想知道,它是如何工作的?我猜第一个循环发送了http请求?但它之后是一个循环内的循环,使用具有看似未记录标志的函数。
I would like to download +=70 urls +=in parallel.
我想同时下载 +=70 个网址 +=。
http://www.php.net/manual/en/function.curl-multi-exec.php
http://www.php.net/manual/en/function.curl-multi-exec.php
<?php
...
$active = null;
//execute the handles
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
...
?>
采纳答案by sectus
You can explore two article that describes this example.
您可以浏览描述此示例的两篇文章。
First, here's the high level. There are two outer loops. The first one is responsible for clearing out the curl buffer right now. The second one is responsible for waiting for more information, and then getting that information. This is an example of what is called blocking I/O. We block execution of the rest of the program until the network I/O is done. While this isn't the most preferable way in general to handle network I/O, it's really our only choice in single-threaded, synchronous PHP.
首先,这是高水平。有两个外环。第一个负责立即清除 curl 缓冲区。第二个负责等待更多信息,然后获取该信息。这是所谓的阻塞 I/O 的一个示例。我们阻止程序其余部分的执行,直到网络 I/O 完成。虽然这通常不是处理网络 I/O 的最佳方式,但它确实是我们在单线程、同步 PHP 中的唯一选择。
Doing curl_multi_exec the right way
First the
$mrcvariable and from the manual we learn that the response is a cURL code defined in the cURL Predefined Constants. In essence it is a regular response and as with any other PHP functioncurl_multi_execis no different and only returns a response once it is finished. Which means there should be only ONE response. In a perfect world this single response is 0 (zero) or equal to the predefined constantCURLM_OK.
首先是
$mrc变量,从手册中我们了解到响应是在 cURL 预定义常量中定义的 cURL 代码。本质上,它是一个常规响应,与任何其他 PHP 函数curl_multi_exec没有什么不同,只有在完成后才返回响应。这意味着应该只有一个响应。在一个完美的世界中,这个单一的响应是 0(零)或等于预定义的常数CURLM_OK。

