我想创建一个可以捕捉最大执行时间的函数,我找到了这个函数。
function runfunction($func,$args,$msgs){
try {
$t1 = time();
$result = "";
while (true) {
$time_2 = time();
$result = call_user_func_array($func, $args);
if($result){ return $result; }
$time_spent = time() - $t1;
$time_funcs = time() - $time_2;
getThrow($time_spent,$time_funcs,$msgs);
}
} catch(Exception $e) {
echo "<html>\n<head>\n<title>Time Execution</title>\n</head>\n";
echo "<body style='font-family: monospace; cursor: default'>\n";
echo 'Caught exception : ', $e->getMessage(), "\n";
echo "</body>\n</html>";
exit(1);
}
}
function getThrow($param1,$param2,$msgs){
if($param2 >= $this->time_sublimit($param1)) {
throw new Exception($msgs);
}
}
上面函数的概念是获取执行函数的时间长度,然后与实际时间进行比较。
问题是当进程在此函数中停留超过 30 秒时。
$result = call_user_func_array($func, $args);
我仍然遇到这个错误。
Fatal error: Maximum execution time of 30 seconds exceeded
当进程仍在该函数内时,是否有可能捕获最大执行时间。
$result = call_user_func_array($func, $args);
在这种情况下,我使用的是 sqlsrv_connect 函数。
runfunction("sqlsrv_connect", array($this->host, $connectionInfo), $errormessage);
我将最长执行时间设置为 30 秒。
最佳答案
PHP 默认执行时间为 30 秒。
如果您只需要更改当前脚本的执行时间,您可以通过在脚本顶部给出以下行来进行更改。
set_time_limit(0);
关于php - 捕获最大执行时间php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34985378/