Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does sleep time count for execution time limit?

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?

like image 959
caw Avatar asked Apr 11 '09 23:04

caw


People also ask

What is execution time limit?

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).

What is the maximum time limit of the sleep command?

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.


2 Answers

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.

like image 176
Samuel Avatar answered Sep 18 '22 12:09

Samuel


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.

like image 36
cletus Avatar answered Sep 18 '22 12:09

cletus