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

雨燕双飞 提交于 2019-11-28 17:20:56

问题


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 pitfalls?


回答1:


One place where it finds use is to create a delay.

Lets say you've built a crawler that uses curl/file_get_contents to get remote pages. Now you don't want to bombard the remote server with too many requests in short time. So you introduce a delay between consecutive requests.

sleep takes the argument in seconds, its friend usleep takes arguments in microseconds and is more suitable in some cases.




回答2:


Another example: You're running some sort of batch process that makes heavy use of a resource. Maybe you're walking the database of 9,000,000 book titles and updating about 10% of them. That process has to run in the middle of the day, but there are so many updates to be done that running your batch program drags the database server down to a crawl for other users.

So you modify the batch process to submit, say, 1000 updates, then sleep for 5 seconds to give the database server a chance to finish processing any requests from other users that have backed up.




回答3:


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.




回答4:


Old question I know, but another reason for using u/sleep can be when you are writing security/cryptography code, such as an authentication script. A couple of examples:

  1. You may wish to reduce the effectiveness of a potential brute force attack by making your login script purposefully slow, especially after a few failed attempts.
  2. Also you might wish to add an artificial delay during encryption to mitigate against timing attacks. I know that the chances are slim that you're going to be writing such in-depth encryption code in a language like PHP, but still valid I reckon.

EDIT

Using u/sleep against timing attacks is not a good solution. You can still get the important data in a timing attack, you just need more samples to filter out the noise that u/sleep adds.

You can find more information about this topic in: Could a random sleep prevent timing attacks?




回答5:


You can use sleep to pause the script execution... for example to delay an AJAX call by server side or implement an observer. You can also use it to simulate delays.

I use that also to delay sendmail() & co. .

Somebody uses use sleep() to prevent DoS and login brutefoces, I do not agree 'cause in this you need to add some checks to prevent the user from running multiple times.

Check also usleep.




回答6:


I had to use it recently when I was utilising Google's Geolocation API. Every address in a loop needed to call Google's server so it needed a bit of time to receive a response. I used usleep(500000) to give everything involved enough time.




回答7:


I wouldn't typically use it for serving web pages, but it's useful for command line scripts.

$ready = false;
do {
  $ready = some_monitor_function();
  sleep(2);
} while (!$ready);



回答8:


Super old posts, but I thought I would comment as well. I recently had to check for a VERY long running process that created some files. So I made a function that iterates over a cURL function. If the file I'm looking for doesn't exist, I sleep the php file, and check again in a bit:

function remoteFileExists() {
    $curl = curl_init('domain.com/file.ext');

    //don't fetch the actual page, you only want to check the connection is ok
    curl_setopt($curl, CURLOPT_NOBODY, true);

    //do request
    $result = curl_exec($curl);

    //if request did not fail
    if ($result !== false) {
        //if request was ok, check response code
        $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);  

        if ($statusCode == 404) {
              sleep(7);
              remoteFileExists();
        }
        else{
            echo 'exists'; 
        }
    }

    curl_close($curl);

}

echo remoteFileExists();



回答9:


Another way to use it: if you want to execute a cronjob more often there every minute. I use the following code for this:

sleep(30);
include 'cronjob.php';

I call this file, and cronjob.php every minute.




回答10:


This is a bit of an odd case...file transfer throttling.

In a file transfer service we ran a long time ago, the files were served from 10Mbps uplink servers. To prevent the network from bogging down, the download script tracked how many users were downloading at once, and then calculated how many bytes it could send per second per user. It would send part of this amount, then sleep a moment (1/4 second, I think) then send more...etc.

In this way, the servers ran continuously at about 9.5Mbps, without having uplink saturation issues...and always dynamically adjusting speeds of the downloads.

I wouldn't do it this way, or in PHP, now...but it worked great at the time.




回答11:


One of its application is, if I am sending mails by a script to 100+ customers then this operation will take maximum 1-2 seconds thus most of the website like hotmail and yahoo consider it as spam, so to avoid this we need to use some delay in execution after every mail.




回答12:


Among the others: you are testing a web application that makes ayncronous requests (AJAX calls, lazy image loading,...)

You are testing it locally so responses are immediate since there is only one user (you) and no network latency.

Using sleep lets you see/test how the web app behaves when load and network cause delay on requests.




回答13:


A quick pseudo code example of where you may not want to get millions of alert emails for a single event but you want your script to keep running.

  if CheckSystemCPU() > 95  
        SendMeAnEmail()
        sleep(1800)
  fi


来源:https://stackoverflow.com/questions/3930736/what-is-a-practical-use-for-phps-sleep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!