Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a timeout for a specific function / block of code (not the whole script)?

I have php scripts that call perl scripts to do various things and sometimes I get it where it just goes on and on without getting a response back, this is based on the variable that is being passed to the perl script and I am doing a lot of different ones in succession so I can't get really debug it directly since I don't have a response from perl...

I would really like to just be able to set a php function or block of code to timeout after a certain number of seconds.. I have been searching on this but haven't found anything yet on how to do this,

I was thinking something like this could work but I don't think it would dynamically update the $time variable, but maybe there is a way to get this to work? Any advice is appreciated

$time = time();
$timeout = $time + 5; //just as an example

do {

// do stuff
} while ($time < $timeout)
like image 657
Rick Avatar asked Aug 06 '10 01:08

Rick


1 Answers

Your best bet would be to use proc_open, sleep for your timeout amount and then call proc_terminate if the process still hasn't completed.

See http://us3.php.net/manual/en/book.exec.php for details on the proc_* family.

like image 100
speshak Avatar answered Oct 14 '22 01:10

speshak