phpcurlcurl-multi

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


Solution

  • 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.