Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Re: (OT?) Output buffering w/Apache

by bastard (Hermit)
on Apr 11, 2001 at 23:47 UTC ( [id://71830]=note: print w/replies, xml ) Need Help??


in reply to Re: (OT?) Output buffering w/Apache
in thread (OT?) Output buffering w/Apache

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>";

Replies are listed 'Best First'.
Re: Re: Re: (OT?) Output buffering w/Apache
by voyager (Friar) on Apr 12, 2001 at 00:30 UTC
    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.
      Ok, i think I know what the problem is now.
      I tested my code (guess i was too lazy before).

      The problem seems to be the size of the resulting display data. 1 to 5 just wan't enough. I changed the code to iterate from 1 to 500000 and removed the sleep (it works with the sleep, but for me 66 lines had to be printed before anything would display).

      My guess is that the browser also has a buffer. The amount of data sent must first fill this buffer before the browser will attempt to display the data it has recieved.

      I also discovered when testing my suggestion that on the command line the script buffers the results until it is complete (on the 1-5 iteration). Adding the $| = 1; you had in the code originally helped this. It doesn't seem to make a difference with the large incrementation set.

      I know it's not line by line output, but with straight cgi it's probably the best that can be had. Hope it helps.

      3 buffers: (+ how to defeat)

      • Webserver
        nph-scriptname.pl
        CGI->nph(1)
        $q->header(-nph=>1)
      • Perl
        $| = 1
      • Browser
        probably can't affect this

      P.S.- as i finish this the test script is on "Line 854"

        I bet the webserver has a buffer. q-: You can test this by using telnet to talk directly to the webserver, type in your GET request (followed by two newlines), and watch. This removes the browser from the equation.

                - tye (but my friends call me "Tye")
        ++ to you bastard. Problem solved. In my case (NT/Apache) I didn't need the nph OR the $|. Clearly I was pumping enough data that even the perl buffers got written. So if is a timing issue on the server, the $| = 1; is needed. In my case, I just wanted to make sure that if there was lots of data, it would begin to appear before it all arrived. Lesson learned: put enough data in the pipe to make a difference. Thanks again.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://71830]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-18 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found