簡體   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