What is the practical use for PHP sleep ()?

I just looked at the docs on sleep() .

Where would you use this feature?

Is it possible for the processor to stop rising in price?

Any common mistakes?

+43
function php sleep usleep
Oct 14 '10 at 6:46 a.m.
source share
13 answers

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

Suppose you create a crawler that uses curl / file_get_contents to retrieve deleted pages. Now you do not want to throw too many requests on a remote server in a short time. This way you introduce a delay between consecutive requests.

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

+43
Oct. 14 '10 at 6:53
source share

Another example: you start some kind of batch process that uses a resource very much. Perhaps you go into a database of 9,000,000 book titles and update about 10% of them. This process should start in the middle of the day, but there are so many updates that need to be done to run the batch program by dragging the database server to bypass for other users.

So, you change the batch process to send, say, 1000 updates, and then sleep for 5 seconds to give the database server the ability to complete processing of any requests from other backups.

+25
Oct. 15 '10 at 3:45
source share

Here is 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 not block Google maps, because I am sending too many requests to the server.

Edit: this is an example of what codaddict says.

+22
Oct 14 '10 at 6:58
source share

An old question that I know, but another reason to use u / sleep might be when you write security / cryptography code like authentication script. A few examples:

+6
Oct 19 '13 at 20:58
source share

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

I use this also to delay sendmail () and co ..

Someone uses the sleep () function to prevent DoS and login brutefoces, I do not agree, because in this you need to add some checks to prevent the user from starting several times.

Also check usleep .

+1
Oct. 14 '10 at 7:02
source share

I had to use it recently when I used the Google Geolocation API. Each address in the cycle needed to call a Google server to get a response. I used usleep(500000) so that everything is used enough time.

+1
Oct 23 '11 at 17:01
source share

I would not use it to serve web pages, but it is useful for command line scripts.

 $ready = false; do { $ready = some_monitor_function(); sleep(2); } while (!$ready); 
+1
Dec 13 '12 at 19:59
source share

Super old posts, but I thought I would comment too. I recently had to check on a VERY lengthy process that created some files. So I made a function that iterates over the cURL function. If the file I'm looking for does not exist, I sleep the php file and repeat the bit 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(); 
+1
Aug 17 '13 at 7:43 on
source share

One of his applications: if I send letters from a script to 100+ clients, then this operation will take a maximum of 1-2 seconds, so most websites such as hotmail and yahoo consider this to be spam, so to avoid this we need to use some delay in execution after each mail.

0
Sep 19 '14 at 13:17
source share

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

You test it locally, so the answers are executed immediately, since there is only one user (you) and there is no network delay.

Using sleep allows you to see / test the behavior of a web application during loading and network failure upon requests.

0
Oct 25 '16 at 19:10
source share

An example of a quick pseudo code where you may not want to receive millions of warning messages for a single event, but you want your script to continue to work.

  if CheckSystemCPU() > 95 SendMeAnEmail() sleep(1800) fi 
0
Nov 22 '16 at 2:24
source share

This is a bit of a strange case ... file throttling.

In the file transfer service that we launched many years ago, files were sent from 10 Mbps upstream servers. To prevent network linking, the script download tracked how many users downloaded at a time, and then counted how many bytes it could send per second to the user. He would send part of this amount, and then a sleeping moment (1/4 second, I think), then send more ... etc.

Thus, the servers worked continuously at approximately 9.5 Mbps without problems with uplink saturation ... and always dynamically changed the download speed.

I would not do it this way or in PHP, now ... but it worked fine at the time.

0
Mar 28 '17 at 21:58
source share

Another use: if you want to do a cronjob more often than not every minute. For this, I use the following code:

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

I call this file and cronjob.php every minute.

-one
Dec 06 '16 at 19:54
source share



All Articles