|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2000-03-14 11:27 UTC] wico at cnh dot nl
Hiya,
i did like to see a function wich breaks into another when it take to long:
<?
funtion Hour () {
sleep 3600;
}
// break after say 2 secs
$timeout = Timeout_function(hour(), 2);
if ($timeout) {
// function took to long
} else {
// function compledet normal
}
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 02:00:01 2025 UTC |
Yes you can do this using ticks, because you can throw an exception from the function to interrupt the script's flow. Here's an example. I have testes it (PHP 5.4.0) and it worked very well. ------------------ // exception class and function class TimeoutException extends Exception {} function time_limit($end_time){ if(time() >= $end_time) throw new TimeoutException; } // usage example register_tick_function('time_limit', time() + 2); // time limit is 2 seconds try { // here goes the stuff we need to limit declare(ticks = 20) { // comment out either line to test /* this line will cause timeout */ while(true) { $foo = 1 + 1; } /* this line would finish in time */ sleep(1); } // if the script gets here the stuff finished in time unregister_tick_function('time_limit'); echo "finished in time"; } catch(TimeoutException $e) { // if the script gets here the stuff took too long unregister_tick_function('time_limit'); echo "took too long"; }