How to set timeout for specific executions in PHP?
What I am trying to do is, I am running queries against Twitter and if the deliver of the result delays more than a specific time, than I want to say to user that "Please try again later, timeout." I am not asking the Twitter part, I told my story because of the initial explanation bu开发者_如何学运维t I like to learn how to accomplish it with mostly PHP native codes.
What's in my mind would be something below:
startTimeout(callback(),3000);//Probably this function needs to start a new thread or another concurrent execution scope
//... initiating some twitter queries here
function callback()
{
echo "Time out, sorry";
}
set_time_limit() should do the trick. This defines the number of seconds that the script can run.
For your purpose, if you use CURL, try
$timeout = 10;
curl_setopt($resource, CURLOPT_TIMEOUT, $timeout);
for other functionality, it's not so easy to accomplish this.
There are function which already support TIMEOUT parameter, it depends on your purpose
精彩评论