检测运行时的最大执行时间
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这并不容易实现。
您可以在脚本开头保存当前时间,然后调用一个函数每隔几行检查一次时间差。但这不是很有效,而且会使您的代码变得混乱。
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.
这是不可能的,因为超过你的最大执行时间将导致 php 出现致命错误
It can't be done because exceeding your max execution time will result in a fatal error in php
如果您将其设置为小于 max 的值,pcntl_alarm 可能会有所帮助执行时间。
pcntl_alarm may help if you set it to value smaller than max execution time.
我想那就没办法了。我只是将时间限制设置为无限。这样它应该会在某个时候结束..:)
I guess no way then. I just set the time limit to infinite. That way it should end sometime.. :)