4

当我运行下面的代码时,在我看来curl_multi_select并且curl_multi_info_read彼此矛盾。据我了解,它curl_multi_select应该是阻塞的,直到curl_multi_exec有响应,但我还没有看到实际发生。

$url = "http://google.com";
$ch  = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);

$mc = curl_multi_init();
curl_multi_add_handle($mc, $ch);

do {
  $exec = curl_multi_exec($mc, $running);
} while ($exec == CURLM_CALL_MULTI_PERFORM);

$ready=curl_multi_select($mc, 100);
var_dump($ready);

$info = curl_multi_info_read($mc,$msgs);
var_dump($info);

这返回

int 1
boolean false

这似乎自相矛盾。它怎么能准备好并且没有任何消息?

我使用的 php 版本是 5.3.9

4

2 回答 2

5

Basically curl_multi_select blocks until there is something to read or send with curl_multi_exec. If you loop around curl_multi_exec without using curl_multi_select this will eat up 100% of a CPU core. So curl_multi_info_read is used to check if any transfer has ended (correctly or with an error).

Code using the multi handle should follow the following pattern:

do
{
    $mrc = curl_multi_exec($this->mh, $active);
}
while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK)
{
    curl_multi_select($this->mh);
    do
    {
        $mrc = curl_multi_exec($this->mh, $active);
    }
    while ($mrc == CURLM_CALL_MULTI_PERFORM);
    while ($info = curl_multi_info_read($this->mh))
    {
        $this->process_ch($info);
    }
}

See also: Doing curl_multi_exec the right way.

于 2012-05-05T18:27:06.290 回答
0

规格

询问多手柄是否有来自个人传输的任何消息或信息。消息可能包含诸如来自传输的错误代码之类的信息,或者只是传输完成的事实。

1 可能意味着有活动,但不一定有消息等待:在这种情况下,可能您的一些下载数据可用,但不是全部。curl_multi_select文档中的示例显式测试从curl_multi_info_read.

于 2012-04-13T22:26:57.557 回答