php 超时 - set_time_limit(0); - 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11999678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:37:30  来源:igfitidea点击:

php timeout - set_time_limit(0); - don't work

phptimeoutexecution

提问by user614963

I'm having a problem with my PHP file that takes more than 30 seconds to execute.

我的 PHP 文件出现问题,执行时间超过 30 秒。

After searching, I added set_time_limit(0);at the start of the code,cbut the file still times out with a 500 errorafter 30 seconds.

搜索后,我set_time_limit(0);在代码的开头添加,但是文件500 error在 30 秒后仍然超时。

log: PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /xxx/xx/xxx.php

safe-mode : off

回答by user1601782

Check the php.ini

检查 php.ini

ini_set('max_execution_time', 300); //300 seconds = 5 minutes

ini_set('max_execution_time', 0); //0=NOLIMIT

回答by Paul

This is an old thread, but I thought I would post this link, as it helped me quite a bit on this issue. Essentially what it's saying is the server configuration can override the php config. From the article:

这是一个旧线程,但我想我会发布此链接,因为它在这个问题上对我帮助很大。基本上它说的是服务器配置可以覆盖 php 配置。从文章:

For example mod_fastcgi has an option called "-idle-timeout" which controls the idle time of the script. So if the script does not output anything to the fastcgi handler for that many seconds then fastcgi would terminate it. The setup is somewhat like this:

例如 mod_fastcgi 有一个名为“-idle-timeout”的选项,它控制脚本的空闲时间。因此,如果脚本在那几秒钟内没有向 fastcgi 处理程序输出任何内容,那么 fastcgi 将终止它。设置有点像这样:

Apache <-> mod_fastcgi <-> php processes

The article has other examples and further explanation. Hope this helps somebody else.

文章还有其他的例子和进一步的解释。希望这对其他人有帮助。

回答by SteJ

I usually use set_time_limit(30) within the main loop (so each loop iteration is limited to 30 seconds rather than the whole script).

我通常在主循环中使用 set_time_limit(30) (因此每个循环迭代被限制为 30 秒而不是整个脚本)。

I do this in multiple database update scripts, which routinely take several minutes to complete but less than a second for each iteration - keeping the 30 second limit means the script won't get stuck in an infinite loop if I am stupid enough to create one.

我在多个数据库更新脚本中执行此操作,这些脚本通常需要几分钟才能完成,但每次迭代不到一秒 - 保持 30 秒限制意味着如果我愚蠢到创建一个脚本,脚本不会陷入无限循环.

I must admit that my choice of 30 seconds for the limit is somewhat arbitrary - my scripts could actually get away with 2 seconds instead, but I feel more comfortable with 30 seconds given the actual application - of course you could use whatever value you feel is suitable.

我必须承认,我选择 30 秒作为限制有点武断——我的脚本实际上可以用 2 秒来代替,但考虑到实际应用,我觉得 30 秒更舒服——当然你可以使用任何你觉得的值合适的。

Hope this helps!

希望这可以帮助!

回答by Anooj P

ini_set('max_execution_time', 300);

use this

用这个

回答by Vins

Checkout this, This is from PHP MANUAL, This may help you.

签出这个,这是来自PHP MANUAL,这可能对你有帮助。

If you're using PHP_CLI SAPI and getting error "Maximum execution time of N seconds exceeded" where N is an integer value, try to call set_time_limit(0) every M seconds or every iteration. For example:

如果您使用 PHP_CLI SAPI 并收到错误“超出 N 秒的最大执行时间”,其中 N 是一个整数值,请尝试每 M 秒或每次迭代调用一次 set_time_limit(0)。例如:

<?php

require_once('db.php');

$stmt = $db->query($sql);

while ($row = $stmt->fetchRow()) {
    set_time_limit(0);
    // your code here
}

?>