开发者

Detecting a timeout for a block of code in PHP

Is there a way you can abort a block of code if it's taking too long in PHP? Perhaps something like:

//Set the max time to 2 seconds
$time = new TimeOut(2);
$time->startTime();

sleep(3)

$time->endTime();开发者_运维技巧
if ($time->timeExpired()){
    echo 'This function took too long to execute and was aborted.';
} 

It doesn't have to be exactly like above, but are there any native PHP functions or classes that do something like this?

Edit: Ben Lee's answer with pcnt_fork would be the perfect solution except that it's not available for Windows. Is there any other way to accomplish this with PHP that works for Windows and Linux, but doesn't require an external library?

Edit 2: XzKto's solution works in some cases, but not consistently and I can't seem to catch the exception, no matter what I try. The use case is detecting a timeout for a unit test. If the test times out, I want to terminate it and then move on to the next test.


You can do this by forking the process, and then using the parent process to monitor the child process. pcntl_fork is a method that forks the process, so you have two nearly identical programs in memory running in parallel. The only difference is that in one process, the parent, pcntl_fork returns a positive integer which corresponds to the process id of the child process. And in the other process, the child, pcntl_fork returns 0.

Here's an example:

$pid = pcntl_fork();
if ($pid == 0) {
    // this is the child process
} else {
    // this is the parent process, and we know the child process id is in $pid
}

That's the basic structure. Next step is to add a process expiration. Your stuff will run in the child process, and the parent process will be responsible only for monitoring and timing the child process. But in order for one process (the parent) to kill another (the child), there needs to be a signal. Signals are how processes communicate, and the signal that means "you should end immediately" is SIGKILL. You can send this signal using posix_kill. So the parent should just wait 2 seconds then kill the child, like so:

$pid = pcntl_fork();
if ($pid == 0) {
    // this is the child process
    // run your potentially time-consuming method
} else {
    // this is the parent process, and we know the child process id is in $pid
    sleep(2); // wait 2 seconds
    posix_kill($pid, SIGKILL); // then kill the child
}


You can't really do that if you script pauses on one command (for example sleep()) besides forking, but there are a lot of work arounds for special cases: like asynchronous queries if you programm pauses on DB query, proc_open if you programm pauses at some external execution etc. Unfortunately they are all different so there is no general solution.

If you script waits for a long loop/many lines of code you can do a dirty trick like this:

declare(ticks=1);

class Timouter {

    private static $start_time = false,
    $timeout;

    public static function start($timeout) {
        self::$start_time = microtime(true);
        self::$timeout = (float) $timeout;
        register_tick_function(array('Timouter', 'tick'));
    }

    public static function end() {
        unregister_tick_function(array('Timouter', 'tick'));
    }

    public static function tick() {
        if ((microtime(true) - self::$start_time) > self::$timeout)
            throw new Exception;
    }

}

//Main code
try {
    //Start timeout
    Timouter::start(3);

    //Some long code to execute that you want to set timeout for.
    while (1);
} catch (Exception $e) {
    Timouter::end();
    echo "Timeouted!";
}

but I don't think it is very good. If you specify the exact case I think we can help you better.


This is an old question, and has probably been solved many times by now, but for people looking for an easy way to solve this problem, there is a library now: PHP Invoker.


You can use declare function if the execution time exceeds the limits. http://www.php.net/manual/en/control-structures.declare.php

Here a code example of how to use

define("MAX_EXECUTION_TIME", 2); # seconds

$timeline = time() + MAX_EXECUTION_TIME;

function check_timeout()
{
    if( time() < $GLOBALS['timeline'] ) return;
    # timeout reached:
    print "Timeout!".PHP_EOL;
    exit;
}

register_tick_function("check_timeout");
$data = "";

declare( ticks=1 ){
    # here the process that might require long execution time
    sleep(5); // Comment this line to see this data text
    $data = "Long process result".PHP_EOL;
}

# Ok, process completed, output the result:
print $data;

With this code you will see the timeout message. If you want to get the Long process result inside the declare block you can just remove the sleep(5) line or increase the Max Execution Time declared at the start of the script


What about set-time-limit if you are not in the safe mode.


Cooked this up in about two minutes, I forgot to call $time->startTime(); so I don't really know exactly how long it took ;)

class TimeOut{
    public function __construct($time=0)
    {
        $this->limit = $time;
    }

    public function startTime()
    {
        $this->old = microtime(true);
    }

    public function checkTime()
    {
        $this->new = microtime(true);
    }

    public function timeExpired()
    {
        $this->checkTime();
        return ($this->new - $this->old > $this->limit);
    }

}

And the demo.

I don't really get what your endTime() call does, so I made checkTime() instead, which also serves no real purpose but to update the internal values. timeExpired() calls it automatically because it would sure stink if you forgot to call checkTime() and it was using the old times.


You can also use a 2nd script that has the pause code in it that is executed via a curl call with a timeout set. The other obvious solution is to fix the cause of the pause.


Here is my way to do that. Thanks to others answers:

<?php
class Timeouter
{
   private static $start_time = FALSE, $timeout;

   /**
    * @param   integer $seconds Time in seconds
    * @param null      $error_msg
    */
   public static function limit($seconds, $error_msg = NULL)
   : void
   {
      self::$start_time = microtime(TRUE);
      self::$timeout    = (float) $seconds;
      register_tick_function([ self::class, 'tick' ], $error_msg);
   }

   public static function end()
   : void
   {
      unregister_tick_function([ self::class, 'tick' ]);
   }

   public static function tick($error)
   : void
   {
      if ((microtime(TRUE) - self::$start_time) > self::$timeout) {
         throw new \RuntimeException($error ?? 'You code took too much time.');
      }
   }

   public static function step()
   : void
   {
      usleep(1);
   }
}

Then you can try like this:

  <?php
  try {
     //Start timeout
     Timeouter::limit(2, 'You code is heavy. Sorry.');

     //Some long code to execute that you want to set timeout for.
     declare(ticks=1) {
        foreach (range(1, 100000) as $x) {
           Timeouter::step(); // Not always necessary
           echo $x . "-";
        }
     }

     Timeouter::end();
  } catch (Exception $e) {
     Timeouter::end();
     echo $e->getMessage(); // 'You code is heavy. Sorry.'
  }


I made a script in php using pcntl_fork and lockfile to control the execution of external calls doing the kill after the timeout.


#!/usr/bin/env php
<?php

if(count($argv)<4){
    print "\n\n\n";
    print "./fork.php PATH \"COMMAND\" TIMEOUT\n"; // TIMEOUT IN SECS
    print "Example:\n";
    print "./fork.php /root/ \"php run.php\" 20";
    print "\n\n\n";
    die;
}

$PATH = $argv[1];
$LOCKFILE = $argv[1].$argv[2].".lock";
$TIMEOUT = (int)$argv[3];
$RUN = $argv[2];

chdir($PATH);


$fp = fopen($LOCKFILE,"w"); 
    if (!flock($fp, LOCK_EX | LOCK_NB)) {
            print "Already Running\n";
            exit();
    }

$tasks = [
  "kill",
  "run",
];

function killChilds($pid,$signal) { 
    exec("ps -ef| awk '\$3 == '$pid' { print  \$2 }'", $output, $ret); 
    if($ret) return 'you need ps, grep, and awk'; 
    while(list(,$t) = each($output)) { 
            if ( $t != $pid && $t != posix_getpid()) { 
                    posix_kill($t, $signal);
            } 
    }    
} 

$pidmaster = getmypid();
print "Add PID: ".(string)$pidmaster." MASTER\n";

foreach ($tasks as $task) {
    $pid = pcntl_fork();

    $pidslave = posix_getpid();
    if($pidslave != $pidmaster){
        print "Add PID: ".(string)$pidslave." ".strtoupper($task)."\n";
    }

  if ($pid == -1) {
    exit("Error forking...\n");
  }
  else if ($pid == 0) {
    execute_task($task);        
        exit();
  }
}

while(pcntl_waitpid(0, $status) != -1);
echo "Do stuff after all parallel execution is complete.\n";
unlink($LOCKFILE);


function execute_task($task_id) {
    global $pidmaster;
    global $TIMEOUT;
    global $RUN;

    if($task_id=='kill'){
        print("SET TIMEOUT = ". (string)$TIMEOUT."\n");
        sleep($TIMEOUT);
        print("FINISHED BY TIMEOUT: ". (string)$TIMEOUT."\n");
        killChilds($pidmaster,SIGTERM);

        die;
  }elseif($task_id=='run'){
        ###############################################
        ### START EXECUTION CODE OR EXTERNAL SCRIPT ###
        ###############################################

            system($RUN);

        ################################    
        ###             END          ###
        ################################
        killChilds($pidmaster,SIGTERM);
        die;
    }
}

Test Script run.php

<?php

$i=0;
while($i<25){
    print "test... $i\n";
    $i++;
    sleep(1);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜