How to exit from function if it takes more than expected time
I have to call this function
$rep_id=$this->getit($domain);
but some domain takes 2/3 minutes I want to go 开发者_如何学Gonext if it take long time. I have set set_time_limit(3000); at the begin of php page
set_time_limit() won't work as that sets the time for the script as a whole. I'm not sure if this is really possible with php but you might be able to pull it off with forking. I'm thinking something along the lines of starting a timer (using time() for a timestamp) and looping until it reaches X time, meanwhile forking your $this->geitit() as a child process. Then when the timer runs out, kill the child process. Might work, but dunno.
I guess maybe an alternative is to make it a separate script with a specified timeout using that set_time_limit() and then call it from a main script using exec()
Assuming the call to getit() is in the main thread (i.e. not being spawned in a new thread), the execution of it will likely block the rest of the script. However, if getit() is self-aware with its own timer, you could design it to dump out if that execution limit is reached, returning an error code indicating the problem. If you decide to embark on this change, consider adding an optional paramater to specify the time limit on call.
精彩评论