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

发布于 2024-09-01 13:14:01 字数 1169 浏览 1 评论 0原文

我在 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;
}
?>

I've seen a lot of questions regarding this error in PHP. But everywhere the solution is given as to set the time limit.

But what I want to do is, to detect the error and if it occurs then switch to another course of action. I mean, if my requests exceed my maximum time set, then it should not give an error message and stop but continue executing some other block of codes.
How may I achieve that?

=======

I just found this code is a site, can it help?

<?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;
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

活雷疯 2024-09-08 13:14:01

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

This is not easily possible.
You could save the current time at the beginning of the script and then call a function which checks the time difference every few lines. This is not very efficient though and it will clutter your code.

药祭#氼 2024-09-08 13:14:01

这是不可能的,因为超过你的最大执行时间将导致 php 出现致命错误

It can't be done because exceeding your max execution time will result in a fatal error in php

笑饮青盏花 2024-09-08 13:14:01

如果您将其设置为小于 max 的值,pcntl_alarm 可能会有所帮助执行时间。

pcntl_alarm may help if you set it to value smaller than max execution time.

忆梦 2024-09-08 13:14:01

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

I guess no way then. I just set the time limit to infinite. That way it should end sometime.. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文