curl_multi_exec必须调用多少次? [英] How many times does curl_multi_exec have to be called?

查看:218
本文介绍了curl_multi_exec必须调用多少次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用curl_multi和multi将文件上传到不同的服务器。每个服务器都有多个需要上传的文件,所以我对每个服务器都有一个curl_multi请求。当我执行curl_multi句柄时,我只是在同一个循环中执行所有的curl_multi句柄,如下:

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

我想知道curl_multi_exec实际上需要在curl请求中调用多少次?是否需要调用每个小数据的数据传输?它使用了很多cpu,我认为它的因为它的繁忙循环太多了。所以我可以添加sleep(5);

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?

我将使用curl_multi_select,但我不能因为多个curl_multi_execs正在处理。

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

推荐答案

curl_multi_exec()读取数据并将数据写入套接字。它不能写或读时停止。
因此,所需的调用次数取决于要传输的数据量和网络的速度。 curl_multi_select()等待套接字变为可读或可写。

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.

您应该使用curl_multi_select()并将所有主机放在一个多句柄中。
如果您需要知道单个请求的持续时间,请选中 curl_getinfo() curl_multi_info_read()

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.

这篇关于curl_multi_exec必须调用多少次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆