Search code examples
phpcurlcurl-multi

How many times does curl_multi_exec have to be called?


I'm using curl_multi with multi to upload files to different servers. Each server has multiple files that need uploading, so I have a curl_multi request for each server. When I execute the curl_multi handles, I just execute all the curl_multi handles in the same loop, like so:

<?php
do {

 $continue_running=false;

 foreach($handles as $host => $handle) {

  if(!is_resource($handle[0])) {
   die("\nHandle is not a resource!\n");
  }

  if($running_{$host}) {
   if(curl_multi_exec($handles[$host][0], $running_{$host}) != CURLM_OK) {
    die("\ncurl_multi_exec failed!\n");
   }
   $continue_running=true;
  }

  if(!$running_{$host} && !$done_{$host}) {
   echo "$host finished in ".(microtime(1)-$start)." seconds\n";
   $done_{$host}=true;
  }
 }

} while ($continue_running);
?>

What I'm wondering is, how many times does curl_multi_exec actually have to be called in a curl request? Does it need to be called for each little bit of data transfered? Its using a lot of cpu and I'm thinking that its because its "busy looping" too much. So can I add sleep(5); at the end of each loop to make it use less cpu cycles, or will this slow down the requests majorly?

I would use curl_multi_select, but I cant because theres multiple curl_multi_execs being processed.


Solution

  • curl_multi_exec() reads and writes data to the socket. It stops when it cannot write or read. So the number of needed calls depends on the amount of data to be transfered and the speed of the network. curl_multi_select() waits till a socket becomes readable or writable.

    You should use the curl_multi_select() and have all hosts inside one multi-handle. Check curl_getinfo() or curl_multi_info_read() if you need to know the duration of the individual requests.