66

I have two questions concerning the sleep() function in PHP:

  1. Does the sleep time affect the maximum execution time limit of my PHP scripts? Sometimes, PHP shows the message "maximum execution time of 30 seconds exceeded". Will this message appear if I use sleep(31)?

  2. Are there any risks when using the sleep()function? Does it cost a lot of CPU performance?

alex
  • 479,566
  • 201
  • 878
  • 984
caw
  • 30,999
  • 61
  • 181
  • 291
  • my guess is it does cound... and why would you need a sleep() in your PHP? just wondering... please say more – Peter Perháč Apr 11 '09 at 23:09
  • The cause: I can only run cronjobs every 5 minutes. So I thought I could use ... $waittime = mt_rand(0, 290); sleep($waittime); ... and execute the code 4 times simultaneously. So I should have a cron at 4 different points in time. – caw Apr 11 '09 at 23:25
  • I startet some scripts with sleep times of more than 100 seconds. Now my website doesn't load any more. Probably the server can only run x scripts simultaneously and there are x scripts sleeping. Could that be true? – caw Apr 11 '09 at 23:28
  • @marco92w Your host might have limited the amount of resources you can use, and they might count the resources in threads. – Thom Wiggers Jan 27 '10 at 15:36

5 Answers5

108

You should try it, just have a script that sleeps for more than your maximum execution time.

<?php
  sleep(ini_get('max_execution_time') + 10);
?>

Spoiler: Under Linux, sleeping time is ignored, but under Windows, it counts as execution time.

Samuel
  • 37,778
  • 11
  • 85
  • 87
  • 3
    I love seeing extremely simple answers that do a better job at illustrating than a giant paragraph. Love the spoiler as well! – Nate I Feb 17 '17 at 17:24
  • 4
    I think your this answer is a good example why one should not simply try out a code snippet but rather ask. – Adam Dec 05 '17 at 10:31
  • I don't like such kind of answers. I'm outside and cannot run any code. Not helpful at all. A simple yes or no had been helpful. – Avatar Mar 26 '22 at 11:30
  • Doing it myself: "Both set_time_limit(...) and ini_set('max_execution_time',...); won't count the time cost of sleep, file_get_contents, shell_exec, mysql_query etc." – Avatar Mar 26 '22 at 11:33
9

From the PHP sleep() page, there's this user-contributed note:

Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), the sleep() function, database queries, etc. is not included when determining the maximum time that the script has been running.

karim79
  • 339,989
  • 67
  • 413
  • 406
8

It only affects script time not system calls like sleep(). There is apparently a bug where on Windows sleep() is included. Max execution time is about real-time, not CPU time or anything like that. You can change it however:

  • max_execution_time directive in your php.ini. This is a global setting;
  • Using ini_set() with the above directive. This will change it only for the currently executing script only for that execution;
  • set_time_limit(): also a local change.

As for the difference between the last two, I believe max_execution_time is a fixed quantity. Running:

ini_set('max_execution_time', 60);

will limit to the script to 60 seconds. If after 20 seconds you call:

set_time_limit(60);

the script will now be limited to 20 + 60 = 80 seconds.

cletus
  • 616,129
  • 168
  • 910
  • 942
  • 1
    ?? There are 6 answers that say roughly the same thing and mine gets downvoted? How does that work? What's wrong with the above? – cletus Apr 11 '09 at 23:20
  • Probably because you changed your answer to be correct after the downvote. – Samuel Apr 11 '09 at 23:21
  • 4
    There's no edited time at the bottom, how could he have changed it? – Calvin Apr 11 '09 at 23:30
  • 1
    I don't know why it was downvoted. I am the questioner and you answered my question well. Thanks! – caw Apr 11 '09 at 23:33
  • @Calvin: In case you haven't noticed, they changed it a few weeks (or days) to give a much larger grace period for no edit history. – Samuel Apr 11 '09 at 23:59
  • They did? How long do you have to edit now? I'm pretty sure it's still the 5 minutes. – Paolo Bergantino Apr 12 '09 at 00:02
0

Others have already covered the basics of sleep() and PHP script execution time limit, but you should also be aware of another risk when using really long sleep periods.

Usually, when a browser sends a request to a server and does not receive any data from the server, the connection can time out. This time limit depends on the browser's configurations, but I've read that IE7 has a default value of just 30 seconds, while Firefox has a default value of 115 seconds--you can check your own configuration in Firefox by going to about:config and filtering for network.http.keep-alive.timeout (the time limit is specified in seconds).

Edit: I had the units for network.http.keep-alive.timeout and browser.urlbar.search.timeout mixed up. It is indeed in seconds, not tenths of a second.

weynhamz
  • 1,968
  • 18
  • 18
Calvin
  • 4,559
  • 1
  • 25
  • 23
  • Thanks, I didn't know that before. I think the limit must be specified in 1/1ths of a second because the value in about:config is 30. ;) – caw Apr 11 '09 at 23:58
  • Ah, you're right. It is in seconds. But the default ought to be 300 seconds, not 30, so perhaps your settings were changed by a browser extension. – Calvin Apr 12 '09 at 00:10
-7

a) Yes, it counts toward the time limit (so sleep(31) will trigger an error)

b) It does the opposite of costing CPU performance - it lets other applications use the CPU (when an application sleeps, the CPU usage of that application will be near 0%). Aside from taking time away from the user, I can't really think of any risks of using this.

v3.
  • 2,003
  • 15
  • 18