PHP 捕获最大执行时间错误

发布于 2024-08-29 18:40:55 字数 49 浏览 7 评论 0原文

PHP 中有没有一种方法可以在达到最大执行时间时捕获致命错误并给用户一个更好的消息?

Is there a way in PHP to trap the fatal error when the max execution time is reached and give the user a better message?

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

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

发布评论

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

评论(4

木格 2024-09-05 18:40:55

阅读此处的前两个答案后,我必须自己测试 register_shutdown_function() ——我认为它不会运行。毕竟,一旦没有更多内存或执行时间已过,用户函数如何运行?我很惊讶地发现关闭函数确实会运行,即使是在 OOM 情况下或超出执行时间时也是如此。概念验证:

测试内存限制:

<?php
function asdf() { echo "omg\n"; }
register_shutdown_function('asdf');

ini_set('memory_limit', '1000');

$x = '';
while(true) {
 $x .= 'lkajsdlfkjasldkfjlaskdfjasldkfj';
}

输出:

PHP Fatal error:  Allowed memory size of 262144 bytes exhausted (tried to allocate 169540 bytes) in /home/scratch.php on line 9
PHP Stack trace:
PHP   1. {main}() /home/scratch.php:0

Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocate 169540 bytes) in /home/scratch.php on line 9

Call Stack:
    0.0002      81360   1. {main}() /home/scratch.php:0

omg

测试执行时间:

cat scratch.php
<?php
function asdf() { echo "omg\n"; }
register_shutdown_function('asdf');

set_time_limit(1);

while(true) {}

输出:

PHP Fatal error:  Maximum execution time of 1 second exceeded in /home/scratch.php on line 7
PHP Stack trace:
PHP   1. {main}() /home/scratch.php:0

Fatal error: Maximum execution time of 1 second exceeded in /home/scratch.php on line 7

Call Stack:
    0.0002      80200   1. {main}() /home/scratch.php:0

omg

请注意,要让消息在 PHP 错误输出之前显示,您必须完全禁用 PHP 错误输出。无论如何,这对于生产站点来说是最佳实践。

After reading the first two answers here, I had to test register_shutdown_function() myself -- I didn't think it'd run. After all, how can a user function run once there's no more memory, or execution time has elapsed? I was surprised to learn that shutdown functions do run, even in an OOM situation, or when execution time has been exceded. Proof of concept:

To test memory limit:

<?php
function asdf() { echo "omg\n"; }
register_shutdown_function('asdf');

ini_set('memory_limit', '1000');

$x = '';
while(true) {
 $x .= 'lkajsdlfkjasldkfjlaskdfjasldkfj';
}

Output:

PHP Fatal error:  Allowed memory size of 262144 bytes exhausted (tried to allocate 169540 bytes) in /home/scratch.php on line 9
PHP Stack trace:
PHP   1. {main}() /home/scratch.php:0

Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocate 169540 bytes) in /home/scratch.php on line 9

Call Stack:
    0.0002      81360   1. {main}() /home/scratch.php:0

omg

To test execution time:

cat scratch.php
<?php
function asdf() { echo "omg\n"; }
register_shutdown_function('asdf');

set_time_limit(1);

while(true) {}

Output:

PHP Fatal error:  Maximum execution time of 1 second exceeded in /home/scratch.php on line 7
PHP Stack trace:
PHP   1. {main}() /home/scratch.php:0

Fatal error: Maximum execution time of 1 second exceeded in /home/scratch.php on line 7

Call Stack:
    0.0002      80200   1. {main}() /home/scratch.php:0

omg

Note that, to get your message to display before PHP's error output, you'd have to disable PHP's error output entirely. Which is best practice for a production site anyway.

倾`听者〃 2024-09-05 18:40:55

尝试在执行文件后设置“完成”标志并注册一个关闭函数来检查该标志是否已定义

Try setting a "Done" flag after the execution of a file and registering a shutdown function that will check if that flag is defined

久隐师 2024-09-05 18:40:55

我原来的答案不起作用,因为您无法捕获致命错误。如果您仍然想阅读它,请检查修订历史记录。

我唯一的猜测是您可以使用register_shutdown_function。在脚本开始时设置一个变量,在脚本成功完成后清除它。编写一个函数来检查该变量并对其进行操作(如果它仍然设置),然后使用 register_shutdown_function 应用该函数

http://php.net/manual/en/function.register-shutdown-function.php

My original answer won't work, as you can't catch a fatal error. If you want to read it anyway, check the revision history.

My only guess is that you could use register_shutdown_function. Set a variable at the start of the script, clear it when the script has completed successfully. Write a function that checks that variable and acts on it if it's still set, then apply the function using register_shutdown_function

http://php.net/manual/en/function.register-shutdown-function.php

你是我的挚爱i 2024-09-05 18:40:55

如果没有办法从 API 级别捕获错误,则 PHP 引擎应该能够在发生致命错误时将内存转储到硬盘上,就像 Windows 在看到“蓝屏”时所做的那样。

If there's no way to trap the error from the API level, the PHP engine should be able to dump the memory on hdd whenever a fatal error occurs, like what Windows does when you see a 'blue screen'.

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