簡體   English   中英

捕獲最大執行時間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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM