检测运行时的最长执行时间

时间:2010-05-13 10:54:27

标签: php execution-time

我在PHP中看到了很多关于这个错误的问题。但到处都有解决方案来设定时限。

但我想要做的是,检测错误以及是否发生错误然后切换到另一个行动方案。我的意思是,如果我的请求超过我设置的最大时间,那么它不应该给出错误消息并停止但是继续执行其他一些代码块。 我怎么能实现这个目标呢?

=======

我刚发现这个代码是一个网站,可以帮忙吗?

<?php

//open error reporting, the error message is required in this script
error_reporting(1);

//for testing, set maximum execution to 1 second
set_time_limit(1);

//turn on output buffering and set callback function
ob_start('callback');

//------------------------------------------------------------------------------

//main program(dead loop)
while (true);

//script completed(impossible)
echo 'Completed!';

//------------------------------------------------------------------------------

/**
 * output callback, execute when end of the request
 *
 * @param string $buffer
 * @return string
 */
function callback($buffer) {
    if (false !== (stripos($buffer,'<b>Fatal error</b>'))) {
        if (false !== (stripos($buffer,'Maximum execution'))) {
            $buffer = 'ycTIN.com , Server Busy';
        } else {
            $buffer = 'ycTIN.com , Server Internal Error';
        }
    }
    return $buffer;
}
?>

4 个答案:

答案 0 :(得分:0)

这不容易实现。 您可以将当前时间保存在脚本的开头,然后调用一个函数来检查每几行的时间差。这不是很有效,但它会使你的代码混乱。

答案 1 :(得分:0)

无法完成,因为超过最长执行时间会导致php中的致命错误

答案 2 :(得分:0)

如果将其设置为小于最大执行时间的值,则

pcntl_alarm可能会有所帮助。

答案 3 :(得分:0)

我想当时没办法。我只是将时间限制设置为无限。这样它应该在某个时候结束.. :))