php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #3830 Function to timeout/break off a function
Submitted: 2000-03-14 11:27 UTC Modified: 2013-10-27 15:10 UTC
Votes:36
Avg. Score:4.6 ± 0.9
Reproduced:26 of 28 (92.9%)
Same Version:4 (15.4%)
Same OS:12 (46.2%)
From: wico at cnh dot nl Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
 [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
}

?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-07-31 22:49 UTC] waldschrott@php.net
well, could be quite nice
 [2002-01-28 16:54 UTC] yohgaki@php.net
register_tick_function() can be used.
 [2002-01-28 18:14 UTC] torben@php.net
No, you can't. :) The ticks stuff won't stop a function which
is tied up in a function call such as sleep. This does sound
like something which would be nice to have...

I've also updated the version number on this to 4.2.0-dev


Torben
 [2010-11-19 00:03 UTC] jani@php.net
-Package: Feature/Change Request +Package: *General Issues -Operating System: linux +Operating System: * -PHP Version: 4.2.0-dev +PHP Version: *
 [2010-12-01 15:41 UTC] jani@php.net
-Package: *General Issues +Package: Scripting Engine problem
 [2012-04-20 11:01 UTC] shiranai7 at hotmail dot com
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";

}
 [2012-04-20 11:07 UTC] shiranai7 at hotmail dot com
I am sorry for reviving this 12 years old report. I have completelly overlooked 
the submission date :O
 [2013-10-27 15:10 UTC] krakjoe@php.net
-Status: Open +Status: Not a bug
 [2013-10-27 15:10 UTC] krakjoe@php.net
The information regarding ticks is misleading.

If PHP is engaged in a blocking syscall, which sleep _is_, the process is blocked, control does not return to the script for it to invoke anything.

So, no, this cannot be done, and didn't need to be open for 10 years.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 11:01:27 2024 UTC