加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 使用usleep()节流cURL

发布时间:2020-12-13 16:13:06 所属栏目:PHP教程 来源:网络整理
导读:我正在使用网络服务发送100个http帖子.但是,该服务仅允许每秒5次.我想知道usleep命令是否是最好的方法.例如: foreach($JSONarray['DATABASE'] as $E) { $aws = curl_init(); //curl stuff curl_exec($aws); curl_close($aws); usleep(200000);} 解决方法 现
我正在使用网络服务发送100个http帖子.但是,该服务仅允许每秒5次.我想知道usleep命令是否是最好的方法.例如:

foreach($JSONarray['DATABASE'] as $E) 
{
    $aws = curl_init();
    //curl stuff
    curl_exec($aws);
    curl_close($aws);
    usleep(200000);
}

解决方法

现在这是未经测试的,但它应该为您提供我将要做的事情的想法(也许这个片段就像它一样工作 – 谁知道……):

// 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++;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读