title: curl_multi_exec performance conern
body: <p>The following example is taken from:</p>
  
  <p><a href="http://php.net/manual/en/function.curl-multi-close.php#example-3540" rel="nofollow">http://php.net/manual/en/function.curl-multi-close.php#example-3540</a></p>
  
  <blockquote>
    <p>This example will create two cURL handles, add them to a multi handle,
    and then run them in parallel.</p>
  </blockquote>
  
  <pre><code>&lt;?php
  // create both cURL resources
  $ch1 = curl_init();
  $ch2 = curl_init();
  
  // set URL and other appropriate options
  curl_setopt($ch1, CURLOPT_URL, "http://www.example.com/");
  curl_setopt($ch1, CURLOPT_HEADER, 0);
  curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
  curl_setopt($ch2, CURLOPT_HEADER, 0);
  
  //create the multiple cURL handle
  $mh = curl_multi_init();
  
  //add the two handles
  curl_multi_add_handle($mh,$ch1);
  curl_multi_add_handle($mh,$ch2);
  
  $running=null;
  //execute the handles
  do {
      curl_multi_exec($mh,$running);
  } while ($running &gt; 0);
  
  //close the handles
  curl_multi_remove_handle($mh, $ch1);
  curl_multi_remove_handle($mh, $ch2);
  curl_multi_close($mh);
  
  ?&gt;
  </code></pre>
  
  <p>I adjusted it a bit to achieve my needs. I want to call only one resource but execute local code while it is requested. This works just fine and my performance concern is also valid for the example without modification.</p>
  
  <p>I produced resources that take 5 seconds to deliver and used them as $ch1 and $ch2</p>
  
  <p>As expected the total execution took only 5 seconds instead of 10.</p>
  
  <p>But in the do loop I added a small counter that revealed that curl_multi_exec and respectively the do loop get executed ~5000000 times on my local machine during the ~5 seconds. That would be about one run per microsecond, which is really quite a lot.</p>
  
  <p>I am worried that this occupies extensive CPU resources while just waiting for the requests to finish and basically doing nothing.</p>
  
  <p>I remembered that one of the first things I learned at a Java course was to avoid endless loops and work with Thread sleeps/interrupts instead for the reasons stated above.</p>
  
  <p>However we don't have threads here and I don't know whether this is valid for PHP too.</p>
  
  <p>As this is an official example I thought I rather ask the experts here first.</p>
  
  <p>I thought about implementing a short sleep in the do loop. Somehing like <code>usleep(100)</code>.</p>
  
  <ul>
  <li><p>Is my concern valid?</p>
  
  <ul>
  <li><p>If so, is the sleep solution a good one?</p>
  
  <ul>
  <li>If so, what would be a good sleep interval? I would love something as small as possible so no process gets slowed down, but I also don't want to hammer the server.</li>
  </ul></li>
  <li><p>If not, why?</p></li>
  </ul></li>
  </ul>