Let's add pcntl_signal
and pcntl_alarm
to the list.
With the help of those functions you can work around any set_time_limit restriction created int the php.ini or in the script.
This script for example will run for 10 seconds despite of set_time_limit(1);
(Credit goes to Sebastian Bergmanns tweet and gist:
<?php
declare(ticks = 1);
set_time_limit(1);
function foo() {
for (;;) {}
}
class Invoker_TimeoutException extends RuntimeException {}
class Invoker
{
public function invoke($callable, $timeout)
{
pcntl_signal(SIGALRM, function() { throw new Invoker_TimeoutException; }, TRUE);
pcntl_alarm($timeout);
call_user_func($callable);
}
}
try {
$invoker = new Invoker;
$invoker->invoke('foo', 1);
} catch (Exception $e) {
sleep(10);
echo "Still running despite of the timelimit";
}