Is there a way to timeout a function in PHP if it doesn't return a value in x seconds/microseconds?
For instance, if our primary MySQL database server is down, scripts will wait for 20 seconds while trying to connect. The scripts stack up on our web servers and cause a cascading failure. I need a way for mysql_connect to just return false or stop after one second and then I can write backup logic to display error messages to clients. Any suggestions? I'm aware of the set_time_limit function, but that will terminate the whole script. I need something to just terminate a function call.
TIA!
Function Timeout
Moderator: General Moderators
- dibyendrah
- Forum Contributor
- Posts: 491
- Joined: Wed Oct 19, 2005 5:14 am
- Location: Nepal
- Contact:
You can add a logic in function like below :
I haven't implemented this kind of things, so, I'm lacking logic on this.
Code: Select all
function some_func(){
$start = microtime_float();
//some logic
$end = microtime_float();
if(round($end - $start, 3)<5)
//continue logic
if(round($end - $start, 3)<10)
//continue logic
//continue logic
if(round($end - $start, 3)<20) return false
//continue logic
return output;
}- dibyendrah
- Forum Contributor
- Posts: 491
- Joined: Wed Oct 19, 2005 5:14 am
- Location: Nepal
- Contact: