捕获最大执行时间 php


Catch maximum execution time php

我想创建一个可以捕获最大执行时间的函数,我找到了这个函数。

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);

30 秒是 PHP 的默认执行时间限制,如果要增加它,你必须增加 PHP 的最大时间限制,就像这样

  ini_set('max_execution_time', 300);
300

代表300秒,或者,如果您想为PHP运行的每个脚本增加它,请在php.ini文件中更新它。

转到xampp'php'php.ini

搜索max_execution_time,并更改为

max_execution_time=900