Does sleep time count for execution time limit?
PhpSleepExecutionMaxPhp Problem Overview
I have two questions concerning the sleep()
function in PHP:
-
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)
? -
Are there any risks when using the
sleep()
function? Does it cost a lot of CPU performance?
Php Solutions
Solution 1 - Php
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.
Solution 2 - Php
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][1] directive in your php.ini. This is a global setting;
- Using [ini_set()][2] with the above directive. This will change it only for the currently executing script only for that execution;
- [set_time_limit()][3]: 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. [1]: http://au2.php.net/manual/en/info.configuration.php#ini.max-execution-time [2]: http://au2.php.net/ini_set [3]: http://au2.php.net/set_time_limit
Solution 3 - Php
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.
Solution 4 - Php
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 http://kb.mozillazine.org/Network.http.keep-alive.timeout">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.
Solution 5 - Php
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.