doumeng1089 2011-09-30 05:37
浏览 72

在900秒(15分钟)后混淆PHP / AJAX超时

all of the below is based on Linux, recent Apache, recent PHP module for Apache.

I am experiencing an unexpected behavior for my AJAX+PHP based web-application. It is quite a simple AJAX approach, basically involving a request to a PHP-script (execute.php) that runs for some time (few seconds to several minutes). As a test-setup, the PHP-script (execute.php) basically just sleeps for a certain amount of time. When this amount is 899 seconds, it works, but when I set it to 900, the answer of the PHP-script (simple echo() ) seems to expire or something like this (what is exactly the cause or reason is actually unknown to me) as the request is not taking notice of it and thus not responding and the respective innerHTML is not updated, as expected. Please find the code below.

I am now wondering where this "timeout" of 900 seconds comes from and even more important, how can I prevent that from happening? After all, I thought that this was, at least, one general idea of AJAX to handle asynchronous calls and notify the client as soon as a request has changed its state?!

process.php

<?php
session_start();

include("functions.php");
include("env.php");

html_head("Processing...");

$sid = $_SESSION['my_sid'];

?>
<script type="text/javascript">
function run_analyses(params){
    <?php
    print "var sid = \"". $sid ."\";
    ";
    ?>

    // Use AJAX to execute the programs independenantly in the background
    // Allows for the user to close the process-page and come back at a later point to the results-link, w/o need to wait.
    if (window.XMLHttpRequest)
    {
        http_request = new XMLHttpRequest();
    }
    else
    {
        //Fallback for IE5 and IE6, as these don't support the above writing/code
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //Is http_request still false
    if (!http_request)
    {
        alert('Ende :( Kann keine XMLHTTP-Instanz erzeugen');
    }

    http_request.onreadystatechange=function(){
        if (http_request.readyState==4 && http_request.status==200){
            // Maybe used to display the progress of the execution
            document.getElementById("output").innerHTML=http_request.responseText;
        }
    };

    http_request.open("POST","execute.php",true);
    //Send the proper header information along with the request
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", params.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(params);
};
</script>

<?php
$params "some_params";
print "
Starting AJAX-call<br>
<div style=\"width:400px; border: 1px black solid;\" id=\"output\">Use this to display an information about the progress</div>
<script type=\"text/javascript\">
    run_analyses('".$params."');
</script>
<form action=\"./usersets/". $sid ."/results.html\" id=\"go_to_results\" method=\"POST\">
<input type='hidden' name=\"session_id\" value=\"" .$sid. "\">
</form>
";
html_foot();
?>

and execute.php:

<?php

session_start();

include("functions.php");
include("env.php");

$sid = $_SESSION['my_sid'];

// Used to get the time at start of processing
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
session_write_close();

sleep(900);  //899 here works

session_start();
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 3);
//echo 'Page generated in '.$total_time.' seconds.'."
";

// Make it accessible for the results
$_SESSION['process_time'] = $total_time;

echo("none");

?>

So the text after "Starting AJAX call" is updated to "none" when the PHP-script sleeps for 899 seconds, but stays "Use this to display an information about the progress" if that value is 900 seconds.

I am really looking forward to your suggestions.

Best,

Shadow

EDIT

Doing grep max_execution $(locate php.ini ) gives:

/etc/php5/apache2/php.ini:max_execution_time = 30
/usr/share/doc/php5-common/examples/php.ini-development:max_execution_time = 30
/usr/share/doc/php5-common/examples/php.ini-production:max_execution_time = 30
/usr/share/php5/php.ini-production:max_execution_time = 30
/usr/share/php5/php.ini-production-dist:max_execution_time = 30
/usr/share/php5/php.ini-production.cli:max_execution_time = 30

EDIT2 When calling the scripts locally (apache webserver running on my local linux machine) in my browser interestingly it behaves as expected and respectes the return of the request. But it exhibits the above unexpected behavior when run on a webserver running on a different machine. Any clues on that? Because I don't have any... Sorry.

  • 写回答

3条回答 默认 最新

  • douju1928 2011-09-30 05:43
    关注

    In your php.ini there is a max_execution_time, the default is 30 seconds, so it seems somewhere it has been set to 900. So I would check there first. Info: http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time

    As for solving it, you can edit the php.ini to set that value really high (or 0 for unlimited), or alternatively you might be able to use this function: http://php.net/manual/en/function.set-time-limit.php to reset it after say 300 seconds.

    Hope this helps!

    Edit: added Lee's suggestion of 0 value max_execution_time

    Edit 2:

    Just found something interesting here http://www.php.net/manual/en/function.set-time-limit.php#75557

    Please note that, under Linux, sleeping time is ignored, but under Windows, it counts as execution time.

    So, that could be why its allowed to execute for longer than 30 seconds, but still cutting it off eventually.

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置