捕获最大执行时间php

时间:2016-01-25 04:55:36

标签: 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秒。

3 个答案:

答案 0 :(得分:2)

Php默认执行时间为30秒。

如果您只需要更改当前脚本的执行时间,可以通过在脚本顶部给出以下行来进行更改。

template <const char *&text>
class MyException: public std::exception
{
public:
    virtual const char* what() const { return(text); }
};

const char *Ex1 = "Message1";
const char *Ex2 = "Message2";
const char *Ex3 = "Message3";

int _tmain(int argc, _TCHAR* argv[])
{
    throw MyException<Ex1>();
    throw MyException<Ex2>();
    throw MyException<Ex3>();
    return 0;
}

答案 1 :(得分:0)

30秒是PHP中的默认时间执行限制,如果你想增加它,你必须增加PHP的最大时间限制,如此

  ini_set('max_execution_time', 300);

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

答案 2 :(得分:0)

转到xampp\php\php.ini

搜索max_execution_time,然后更改为

max_execution_time=900