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?
Maximum execution time (max_execution_time) is a time limit on how long a PHP script can run. It is a way hosting providers can limit the use and abuse of server resources, especially for shared hosting. The actual default value depends on the hosting, but it-s usually set to 30 (i.e. 30 seconds).
As with all other utilities that take integral operands and do not specify subranges of allowed values, sleep is required […] to deal with time requests of up to 2147483647 seconds. 2147483647 seconds is over 68 years.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With