12

Using set_time_limit() or max_execution_time, does not "really" limits (except on Windows) the execution time, because as stated in PHP manual:

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(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.

A solution is proposed in PHP comments to have a "real" execution time limit like what I'm looking for, but I found it unclear/confusing.

6
  • 2
    A real execution time limit is set by the system executing the process. It can kill the process if the limit is reached (also known as hard limit). It's a real limit then, normally based on CPU time, that is actual time consumed by a process (not exactly how long the process already runs). Commented Sep 27, 2011 at 13:22
  • 5
    The solution suggested in the comment you are talking about looks good to me - as long as you have POSIX and pcntl functions available. What exactly don't you understand about it? Commented Sep 27, 2011 at 13:23
  • Looking more thoroughly at the suggestion in PHP manual comments was helpful :) I didn't understand at first how it was working (I'm not used to threads handling). Commented Sep 27, 2011 at 14:20
  • 2
    If you need to control some loop based process you can use just simple timer. Commented Sep 28, 2011 at 7:03
  • 1
    You could get pretty close to implementing this with a tick function, but it'd be a hack. php.net/manual/en/function.register-tick-function.php Commented Sep 30, 2011 at 1:11

1 Answer 1

3

I might be wrong, but as far as I understand you ask for explanation of the "PHP comments" solution code.

The trick is to spawn a child process, using pcntl_fork function, which will terminate the original (parent) process after some timeout. Function pcntl_fork returns process id of newly created child process inside a parent process execution thread and zero inside child process execution thread. That means parent process will execute the code under if statement and the child process will execute code under else. And as we can see from the code, the parent process will perform endless loop while child process will wait 5 seconds and then kill his parent. So basically you want to do something like this:

$real_execution_time_limit = 60; // one minute

if (pcntl_fork())
{
    // some long time code which should be
    // terminated after $real_execution_time_limit seconds passed if it's not
    // finished by that time
}
else
{
    sleep($real_execution_time_limit);
    posix_kill(posix_getppid(), SIGKILL); 
}

I hope I've exlained it well. Let me know if you sill have question regarding this solution.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.