php - 捕获最大执行时间php

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

关于php - 捕获最大执行时间php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34985378/

相关文章:

php - Wordpress 插件 Advanced Custom Fields Pro 不保存前端和后端

php - MySQLi 等效的 MySQL 代码

php - extract() 获取类成员

php - 在谷歌应用引擎的laravel coaster cms中使特定文件夹可写

php - 在 Laravel 5 中将错误附加到验证器

php - 从查询中获取 PDO 的列名称

php - 在 Woocommerce 3 中更改购物车商品价格

php - 如何在laravel中获取mysql COUNT查询值

php - 解析 Amazon MWS Scratchpad 响应

php - 如何使用 php 循环遍历 mysql 查询