4

I'm using a web service to send 100's of http posts. However, the service only allows 5 per second. I'm wondering if the usleep command is the best way to do this. For example:

foreach($JSONarray['DATABASE'] as $E) 
{
    $aws = curl_init();
    //curl stuff
    curl_exec($aws);
    curl_close($aws);
    usleep(200000);
}
johnwhitney
  • 93
  • 1
  • 3
  • 12
  • 1
    should be ok, though this'll drift a bit since the request itself is going to take a certain amount of time. if you want to maximize efficiency, you should take into account how long each request took and take that away from the 20,000ms pause. – Marc B Aug 19 '11 at 17:14
  • How about using [`curl_multi`](http://php.net/curl_multi_init), which you can control to to keep a maximum of 5 running simultaneously or starting at any given time? – salathe Aug 19 '11 at 17:45

1 Answers1

2

Now this is untested, but it should provide you with the idea of what I would do(and perhaps this snippet just work as it is - who knows...) :

// presets
$thissecond = time();
$cnt = 0;

foreach($JSONarray['DATABASE'] as $E) 
{
  while ($thissecond == time() && $cnt > 4) { // go into "waiting" when we going to fast
    usleep(100000); // wait .1 second and ask again
  }

  if ($thissecond != time()) { // remember to reset this second and the cnt
    $thissecond = time();
    $cnt = 0;
  }

  // off with the payload
  $aws = curl_init();
  //curl stuf
  curl_exec($aws);
  curl_close($aws);

  // remember to count it all
  $cnt++;
}
CodeReaper
  • 5,988
  • 3
  • 35
  • 56