php处理脚本执行超时

今天有人问题,如果动态处理脚本执行超时,貌似第一想到的是修改php.ini 的max_execution_time

但是如果要动态的处理每个脚本,或者每一类脚本的执行时间,php有一个动态修改执行时间的函数

void set_time_limit ( int $seconds )

Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.

When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out. 

This function has no effect when PHP is running in safe mode. There is no workaround other than turning off safe mode or changing the time limit in the php.ini. 

 

函数默认的是30秒,时间是从 set_time_limit 执行开始算时间

还有就是不能在php 安全模式下起作用

 

附带 处理网络超时,好像有很多像 curl,file_get_contents 都是可以加时间限制的 

curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200); 毫秒级的

$timeout = array(
    'http' => array(
        'timeout' => 5 //设置一个超时时间,单位为秒
    )
);
$ctx = stream_context_create($timeout);
$text = file_get_contents("http://example.com/", 0, $ctx);

 

 

posted on 2012-10-18 19:05  charles小白  阅读(498)  评论(0)    收藏  举报