53

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?

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
alex
  • 479,566
  • 201
  • 878
  • 984

13 Answers13

53

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.

codaddict
  • 445,704
  • 82
  • 492
  • 529
  • 1
    The only thing I could think about too, unless one is using php for other things than having it on a webserver. – Frank Oct 14 '10 at 06:56
  • 4
    More generally: *request throttling*, both incoming and outgoing. – deceze Oct 14 '10 at 06:57
  • I use `sleep` after uploading a file to server to ensure that files are uploaded completely. Without it the script would fail. – Vahid Amiri Sep 22 '16 at 22:35
  • 1
    sleep returns 0 to check if successful but usleep doesn't tell anything. Just thought of including it ! Before you use anything xD – Tilak Madichetti Feb 04 '17 at 02:22
34

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.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
26

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.

Script47
  • 14,230
  • 4
  • 45
  • 66
Mischa
  • 42,876
  • 8
  • 99
  • 111
10

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?

Angry Dan
  • 3,241
  • 2
  • 21
  • 18
  • This is not best practice for preventing these types of attacks. It causes your script to stay in execution, taking up memory and resources. It's better to implement a client-side delay in the application and then automatically shut out any requests that don't respect the delay. A server-side delay provides an easy vector for DOS attacks. Artificial delays are also not best practice for preventing timing attacks either: using timing-safe comparison functions is safer and often uses fewer resources. – cazort Jun 12 '19 at 19:00
  • Re: timing attacks see the answer given by ircmaxell on the page you linked to about the random sleep to prevent timing attacks, it explains my point about those attacks in detail. – cazort Jun 12 '19 at 19:01
  • If future Dan could go back and tell past Dan something useful at the moment he was writing this answer, it would be this: "never write your own security/cryptography code". – Angry Dan Feb 07 '22 at 12:20
2

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.

Tom
  • 31
  • 1
2

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.

XediDC
  • 75
  • 6
1

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);
Octopus
  • 8,075
  • 5
  • 46
  • 66
1

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();
David
  • 2,094
  • 3
  • 30
  • 47
1

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.

Fabio Mora
  • 5,339
  • 2
  • 20
  • 30
1

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.

hohner
  • 11,498
  • 8
  • 49
  • 84
0

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.

0

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.

Paolo
  • 15,233
  • 27
  • 70
  • 91
0

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
Mike Q
  • 6,716
  • 5
  • 55
  • 62