hadees
hadees

Reputation: 1764

Why are curl_multi_select and curl_multi_info_read contradicting each other?

When I run the below code it seems to me curl_multi_select and curl_multi_info_read are contradicting each other. As I understand it curl_multi_select is supposed to be blocking until curl_multi_exec has a response but I haven't seen that actually happen.

$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);

this returns

int 1
boolean false

which seems to contradict itself. How can it be ready and not have any messages?

The php version I'm using is 5.3.9

Upvotes: 6

Views: 5724

Answers (2)

flm
flm

Reputation: 970

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.

Upvotes: 5

Femi
Femi

Reputation: 64700

From the spec:

Ask the multi handle if there are any messages or information from the individual transfers. Messages may include information such as an error code from the transfer or just the fact that a transfer is completed.

The 1 could mean there is activity, but not necessarily a message waiting: in this case probably that some of your download data is available, but not all. The example in the curl_multi_select doc explicitly tests for false values back from curl_multi_info_read.

Upvotes: 0

Related Questions