首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用usleep()调节cURL

使用usleep()调节cURL
EN

Stack Overflow用户
提问于 2011-08-20 00:56:50
回答 1查看 1.2K关注 0票数 4

我正在使用一个网络服务来发送100个http帖子。然而,该服务仅允许每秒5个。我想知道usleep命令是否是实现此目的的最佳方法。例如:

代码语言:javascript
复制
foreach($JSONarray['DATABASE'] as $E) 
{
    $aws = curl_init();
    //curl stuff
    curl_exec($aws);
    curl_close($aws);
    usleep(200000);
}
EN

回答 1

Stack Overflow用户

发布于 2011-08-20 18:28:00

现在这是未经测试的,但它应该为您提供我会做什么的想法(也许这段代码就是这样工作的-谁知道呢……):

代码语言:javascript
复制
// 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++;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7124900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档