5

好吧,所以在过去的 3 个小时里,我一直在为此绞尽脑汁,并且疯狂地在谷歌上搜索,却没有解决问题。因此,我编写了一个示例脚本来重现这一点,因为我的原始脚本大约有 800 行。

<?php
    set_time_limit(0);
    ini_set('max_input_time', '-1');
    ini_set('max_execution_time', '0');

    error_log("executing script... ");
    $time = time();
    for ($t = 0; $t <= 15; $t++) {
        error_log("Logging: $t (".(time()-$time)." seconds)");
        sleep(5);
    }
    error_log("execution done (".(time()-$time)." seconds)");
?>

现在,sleep 函数只是用来重新创建页面的执行时间,并没有在原始代码中使用。这将在 error_log 文件中产生以下结果

[Tue Nov 06 16:24:14 2012] executing script...
[Tue Nov 06 16:24:14 2012] Logging: 0 (0 seconds)
[Tue Nov 06 16:24:19 2012] Logging: 1 (5 seconds)
[Tue Nov 06 16:24:24 2012] Logging: 2 (10 seconds)
[Tue Nov 06 16:24:29 2012] Logging: 3 (15 seconds)
[Tue Nov 06 16:24:34 2012] Logging: 4 (20 seconds)
[Tue Nov 06 16:24:39 2012] Logging: 5 (25 seconds)
[Tue Nov 06 16:24:44 2012] Logging: 6 (30 seconds)
[Tue Nov 06 16:24:49 2012] Logging: 7 (35 seconds)
[Tue Nov 06 16:24:54 2012] Logging: 8 (40 seconds)
[Tue Nov 06 16:24:59 2012] Logging: 9 (45 seconds)
[Tue Nov 06 16:25:04 2012] Logging: 10 (50 seconds)
[Tue Nov 06 16:25:09 2012] Logging: 11 (55 seconds)
[Tue Nov 06 16:25:14 2012] executing script...
[Tue Nov 06 16:25:14 2012] Logging: 0 (0 seconds)
[Tue Nov 06 16:25:14 2012] Logging: 12 (60 seconds)
[Tue Nov 06 16:25:19 2012] Logging: 1 (5 seconds)
[Tue Nov 06 16:25:19 2012] Logging: 13 (65 seconds)
[Tue Nov 06 16:25:24 2012] Logging: 2 (10 seconds)
[Tue Nov 06 16:25:24 2012] Logging: 14 (70 seconds)
[Tue Nov 06 16:25:29 2012] Logging: 3 (15 seconds)
[Tue Nov 06 16:25:29 2012] Logging: 15 (75 seconds)
[Tue Nov 06 16:25:34 2012] Logging: 4 (20 seconds)
[Tue Nov 06 16:25:34 2012] execution done (80 seconds)
[Tue Nov 06 16:25:39 2012] Logging: 5 (25 seconds)
[Tue Nov 06 16:25:44 2012] Logging: 6 (30 seconds)
[Tue Nov 06 16:25:49 2012] Logging: 7 (35 seconds)
[Tue Nov 06 16:25:54 2012] Logging: 8 (40 seconds)
[Tue Nov 06 16:25:59 2012] Logging: 9 (45 seconds)
[Tue Nov 06 16:26:04 2012] Logging: 10 (50 seconds)
[Tue Nov 06 16:26:09 2012] Logging: 11 (55 seconds)
[Tue Nov 06 16:26:14 2012] Logging: 12 (60 seconds)
[Tue Nov 06 16:26:19 2012] Logging: 13 (65 seconds)
[Tue Nov 06 16:26:24 2012] Logging: 14 (70 seconds)
[Tue Nov 06 16:26:29 2012] Logging: 15 (75 seconds)
[Tue Nov 06 16:26:34 2012] execution done (80 seconds)

这是来自 phpinfo 的一些相关设置

=== apache2handler ===
Max Requests: Per Child: 1000 - Keep Alive: on - Max Per Connection: 200
Timeouts: Connection: 300 - Keep-Alive: 2 

=== PHP Version 5.4.5 Core Settings ===
max_execution_time     Local: 0     Master: 30
max_input_time         Local: -1    Master: -1

我已经尝试了我能想到的一切。它似乎是使用 Apache 或 PHP 的服务器设置,因为当我在本地主机上运行相同的脚本时,它会成功执行,就像它应该没有双重负载一样。在它被问到之前,是的,每次我进行 INI 更改或 apache 配置更改时,我都在重新启动 apache。

此外,这里是上面示例的 access_log 条目。

*.*.*.* - - [06/Nov/2012:16:43:56 -0700] "GET /test.php HTTP/1.1" 200 20 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0" *.*.*.* *********.com 80 55050278 1012 321 *.*.*.*
*.*.*.* - - [06/Nov/2012:16:44:56 -0700] "GET /test.php HTTP/1.1" 200 20 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0" *.*.*.* *********.com 80 60054985 1012 321 *.*.*.*

一旦执行时间达到 60 秒,肯定会执行此操作,因为当我将执行时间更改为 55 秒时,它会按原样运行一次。

[Tue Nov 06 16:46:00 2012] executing script...
[Tue Nov 06 16:46:00 2012] Logging: 0 (0 seconds)
[Tue Nov 06 16:46:05 2012] Logging: 1 (5 seconds)
[Tue Nov 06 16:46:10 2012] Logging: 2 (10 seconds)
[Tue Nov 06 16:46:15 2012] Logging: 3 (15 seconds)
[Tue Nov 06 16:46:20 2012] Logging: 4 (20 seconds)
[Tue Nov 06 16:46:25 2012] Logging: 5 (25 seconds)
[Tue Nov 06 16:46:30 2012] Logging: 6 (30 seconds)
[Tue Nov 06 16:46:35 2012] Logging: 7 (35 seconds)
[Tue Nov 06 16:46:40 2012] Logging: 8 (40 seconds)
[Tue Nov 06 16:46:45 2012] Logging: 9 (45 seconds)
[Tue Nov 06 16:46:50 2012] Logging: 10 (50 seconds)
[Tue Nov 06 16:46:55 2012] execution done (55 seconds)

对此的任何帮助将不胜感激!

4

3 回答 3

5

诸如缺少图标之类的东西是否会导致您的脚本被调用两次。请参阅http://www.webdeveloper.com/forum/showthread.php?245809-Script-Running-Twice。如果您将非 www 重定向到 www 或反之亦然,它也可以发生。从非 www 重定向到 www 时,请参阅PHP 在页面加载时执行两次

于 2012-11-07T00:23:32.663 回答
3

只是为了排除它,您是否尝试过在另一个浏览器中进行测试?我只提到它是因为我最近偶然发现了这个:

https://support.mozilla.org/en-US/questions/922783

于 2012-11-07T00:10:36.843 回答
0

作为一种解决方法,也许您可​​以将执行分成两页;完成第一个后,只需加载第二个。这也可以解决某些资源限制,例如 PHP 配置中的低 RAM 预算。

于 2017-08-17T17:16:48.633 回答