voyager has asked for the wisdom of the Perl Monks concerning the following question:

Entire script:
#! /perl/bin/perl -w use strict; $| = 1; print "Content-type: text/html\n\n"; print "<html><body>"; for my $i (1..5) { my $data = "<br>Line $i"; sleep 1; print $data; } print "</body></html>"; </p>
This behaves as expected from command line: a line of output every second.

When run as a cgi script under apache (win32), nothing prints until the whole thing is ready. I can't find anything in my apache docs about turning buffering off.

So how do I get the partial output displayed in the browser as it is produced?

Replies are listed 'Best First'.
Re:(Zigster) (OT?) Output buffering w/Apache
by zigster (Hermit) on Apr 10, 2001 at 18:50 UTC
    As I understand it you cant do this. The browser will wait for the EOF when the socket closes before rendering. If you want dynamic content on the browser look at server push protocols. Have a look here for lots of info on dynamic content.

    UPDATE Quote:

    Server push -- the server sends down a chunk of data; the browser display the data but leaves the connection open; whenever the server wants it sends more data and the browser displays it, leaving the connection open; at some later time the server sends down yet more data and the browser displays it; etc.

    Sounds to be very much what you require. HTH
    --

    Zigster

      Ok, what I really was trying to test was browser behavior with a very large document that was a) formatted as one big table, b) not ... so user would begin to see results asap. But since my browser is on same machine as web-server, I was having to send a rediculous amount of data to see any delay, so I thought I'd try and fake it.

      Update: I was pretty sure that partial results could be displayed because I was this:

      Not exactly rocket science, was it? Still, there are some fine points here. One is that the program returns headers and the top portion of the page before asking AOLserver for a database connection or asking the database to do anything. This kind of construction ensures that users aren't staring at a blank Netscape window. here

Re: (OT?) Output buffering w/Apache
by astanley (Beadle) on Apr 10, 2001 at 18:49 UTC
    The trouble I suspect is not in Apache buffering the output but in the web browsers loading of the output. I suspect for an <HTML> tagged document the browser does not display the contents "as it arrives" but instead holds onto everything until the document is finished being sent and then renders the page. The reason for this would be if the browser tried to render the page as it received data - I believe it would make web designing hazardous to your health. That's my guess and AFAIK when sending a document as text/html with <(/)html> tags surrounding you cannot "turn the buffering off"

    -Adam Stanley
    Nethosters, Inc.
      The major web browsers render pages as they receive data.

      Update: For instance, visit a fairly long listing of books at http://sailor.gutenberg.org/by-title.html Wait until something displays, and then immediately hit the STOP button. (A better example because it takes longer to download is the full text of Jane Austen's Pride and Prejudice.)

      If you then scroll to the bottom of your page and you had hit STOP fast enough you will see that the displayed text breaks off abruptly. That is because the browser (IE5.5/Win98 in my case) rendered the page before it had finished downloading the page.

Re: (OT?) Output buffering w/Apache
by Rhandom (Curate) on Apr 10, 2001 at 19:16 UTC
    It can depend upon a couple of things. May moons ago I tried to get buffering to work properly under Apache. I could never get it to go. Then, at one point we upgraded to newer version of Apache, and suddenly the buffering began to work just as you wanted above. I think the version change was right around the move from a 1.2 Apache to a 1.3 something Apache. All new versions of Apache seem to work fine.

    Newer versions of Apache will do the buffering correctly, but they will do it different from the command line. On the command line, any character sent shows up. In Apache, you need to send it a newline before the buffer is flushed. However, even if the buffer is flushed, that doesn't mean it will show up. If you look at your browser's status, you will see the size of the document get bigger. Sending a newline to the browser is not enough for it to show output. What you will need to do to make a line of text to show up is to include a <br> or <p> or something that will signal the end of a line so that the browser is then able to render it. So make sure each of your lines is properly terminated with a <br>\n and all will be well with your buffering.
Re: (OT?) Output buffering w/Apache
by bastard (Hermit) on Apr 10, 2001 at 19:00 UTC
    This node may be of some help. It discusses Apache output buffering and the various ways of turing it off.
Re: (OT?) Output buffering w/Apache
by voyager (Friar) on Apr 10, 2001 at 19:36 UTC
    Thanks for the responses so far. (btw I'm on Apache 1.3.17)

    When I try the NPH example in the CGI doc I get the following in the error log:

    malformed header from script. Bad header=HTTP/1.1 200 OK:

    Suggestions?

      Try this code and name the script nph-something.pl.
      I haven't tested it, but it should work. The problem with using the nph mode of the webserver with a script is that the script must now supply all of the standard headers itself. Using the CGI header() function automatically provides these for you.
      #! /perl/bin/perl -w use strict; use CGI; my $q = new CGI; print $q->header(-nph=>1); print "<html><body>"; for my $i (1..5) { my $data = "<br>Line $i"; sleep 1; print $data; } print "</body></html>";
        When I first did this I got the same message as above (malformed header ...). I then renamed the file "nph-foo.pl". No server error this time, but still didn't get any results until the whole thing was ready. Oh, well.
Re: (OT?) Output buffering w/Apache
by Anonymous Monk on Jul 28, 2005 at 12:02 UTC
    Has anyone managed to find a solution to this problem?
      I had the same problem with Apache - neither $| = 1; nor the nph solution worked. Finally I found out that mod_deflate buffers the output. If this module is running, try to turn it off - that worked for me.
        Disabling deflate in Apache worked, just as you described. I wish this were in the Apache2 FAQ. Thanks for sharing that.