What is a practical use for PHP's sleep()?

前端 未结 13 2524
时光说笑
时光说笑 2020-12-23 19:17

I just had a look at the docs on sleep().

Where would you use this function?

Is it there to give the CPU a break in an expensive function?

Any common

相关标签:
13条回答
  • 2020-12-23 20:20

    Here's a snippet of how I use sleep in one of my projects:

    foreach($addresses as $address)
    {
      $url = "http://maps.google.com/maps/geo?q={$address}&output=json...etc...";
      $result = file_get_contents($url);
      $geo = json_decode($result, TRUE);
    
      // Do stuff with $geo
    
      sleep(1);
    }
    

    In this case sleep helps me prevent being blocked by Google maps, because I am sending too many requests to the server.

    0 讨论(0)
提交回复
热议问题