繁体   English   中英

Apache服务器超时,带有错误消息“无法显示页面”的PHP脚本

[英]Apache Server Timeout with an error message “The page cannot be displayed” for a PHP script

我正在尝试使用PHP将简单Perl脚本的输出重定向到Web浏览器。 Perl脚本有时需要2-3个小时来执行并将输出显示到屏幕上。 到这个时候,我想Apache Server只会超时并显示上面描述的错误消息。

这是示例代码片段。

 # The tmpOutput.txt contains output of a Perl script.
 # Read a file using an handle.
$handle = popen("tail -f tmpOutput.txt", 'r');

 # Check if read handle has been allocated properly.
if ($handle) {
    # to prevent the code from hanging up when the response is slow.
    stream_set_blocking($handle, FALSE);

    # Set the stream timeout period to 24 hours i.e. 86400 seconds.
    stream_set_timeout($handle, 86400);

    # Get the available stream meta data information.
    $info = stream_get_meta_data($handle);

    # While there's no end of file, read the file contents and redirect them to standard display.
    while((!feof($handle)) && (!$info['timed_out'])) {
        $buffer = fgets($handle);
        echo "$buffer<br/>\n";
        ob_flush();
        flush();

        $info = stream_get_meta_data($handle);
    }

    # Check if some issue while streaming data.
    if ($info['timed_out']) {
        echo 'Connection timed out!';
    }
}

 # Close the file handle.
pclose($handle);

您为什么要尝试通过Web服务器运行它? 您应该以不同的方式运行它。 Web服务器应发信号给Perl脚本以使用startfile或db条目开始。 脚本的包装程序在cron上运行,并寻找起始文件。 看到它时,它将启动perl进程。 perl proc写入另一个Web可访问文件。 在您的Web应用程序指示perl脚本启动后,它将转发到一个页面,该页面使用settimeout每1秒取消一次perl文件的输出。

编辑

考虑使用Ajax,您不必保持与服务器的开放连接,而可以通过大量请求来实现:

    <script type="text/javascript">
        function getXmlHttp()
        {
                    var xmlHttp;
            try{    
                xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
            }
            catch (e){
                try{
                    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
                }
                catch (e){
                    try{
                        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch (e){
                        alert("No AJAX!?");
                        return false;
                    }
                }
            }
            return xmlHttp;
        }

        function Ajax(){
        var xmlHttp = getXmlHttp();
        xmlHttp.onreadystatechange=function(){
            if(xmlHttp.readyState==4){
                document.getElementById('logwindow').innerHTML=xmlHttp.responseText;                    
                setTimeout('Ajax()',1000);
            }
        }
        /* NAME OF OUTPUT FILE HERE */
        xmlHttp.open("GET","session.log?" + Math.random(),true);
        xmlHttp.send(null);
        }


        Ajax();
        </script>

另一种可能性是安排您的perl脚本连续产生输出。

#!/usr/bin/perl
my $heartbeat_pid = fork();
if ($heartbeat_pid == 0) {    # child process to print a newline every 10 minutes
    my $output_freq = 600;
    for (;;) {
        sleep $output_freq;
        print "\n";
    }
}
&the_main_routine_that_takes_a_long_time_to_produce_output();
kill 9, $heartbeat_pid;     # clean up child at end of script

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM