How many times does curl_multi_exec have to be called?

♀尐吖头ヾ 提交于 2019-12-10 23:30:01

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/3595602/how-many-times-does-curl-multi-exec-have-to-be-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!