views:

1898

answers:

6

Hello!

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?

I hope you can answer my questions.

Thanks in advance!

+9  A: 

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
Thanks, I have UNIX so it doesn't count.
You can actually accept his answer, and make a service for the community.
Itay Moav
A: 

Yes, it counts, sleep(60) and set_time_limit(30) will terminate the script after 30 seconds.

Regarding CPU usage - no.

Alix Axel
A: 

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
+1  A: 

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
?? 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
Probably because you changed your answer to be correct after the downvote.
Samuel
There's no edited time at the bottom, how could he have changed it?
Calvin
I don't know why it was downvoted. I am the questioner and you answered my question well. Thanks!
@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
They did? How long do you have to edit now? I'm pretty sure it's still the 5 minutes.
Paolo Bergantino
A: 

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
Just to add, this behavior is exhibited on *nix only. On windows set_time_limit() wins every time.
Crescent Fresh
A: 

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

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