Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

how to implement "tail -f" using CGI

by poomba (Initiate)
on May 16, 2001 at 23:42 UTC ( [id://81046]=perlquestion: print w/replies, xml ) Need Help??

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

I guess my question could be reworded as "how to send unbuffered output to the browser using CGI?" I'm not so much concerned with duplicating a "tail -f", but if I could get that down I'm sure I'll be able to use it elsewhere.

Edit: chipmunk 2001-05-16

Replies are listed 'Best First'.
Re: how to implement
by Hero Zzyzzx (Curate) on May 16, 2001 at 23:48 UTC
    put $|=1; at the top of your script. This will unbuffer output.

    in case that's hard to read, it's "$|=1;"
      That doesn't seem to quite work. I wrote the follow test:
      #!/usr/bin/perl $|=1; print "Content-type: text/html\n\n"; for (1..20) { print "$_<br>\n"; sleep 2; }
      and the browser still waits until all 20 numbers are outputted before returning the page.

      This is under IE 5.0, Perl 5.005 and Apache 1.3.19.

      Update: If I increase the range to something higher, I find that around 40 or so, it's starts to output. Apache itself must be doing some buffering ...

        The guilty party here is IE, not apache. I just compared IE6 to N4.76, both on win2000. IE won't display anything before it has read about 230 bytes. If you then reload the page, there's no buffering.

        quick fix:

        print "<!-- hey IE, display this page already -->\n" x 5;
        --
        Brigitte    'I never met a chocolate I didnt like'    Jellinek
        http://www.horus.com/~bjelli/         http://perlwelt.horus.at

        Sorry for being *nix-centric. I only have done perl coding on linux/unix. I don't know how unbuffering behaves on a windows machine. This works perfectly on my linux boxes. Sorry!

Re: how to implement "tail -f" using CGI
by saucepan (Scribe) on May 17, 2001 at 03:52 UTC
    Try renaming your CGI script to nph-whatever.cgi. Many webservers will, upon noticing the "nph-" (no parsed headers) prefix on a CGI script, send the script output directly to the browser without any buffering.

      This is the "old fashioned" way to do it. Instead, you are probably better off using the CGI.pm way, which is documented as such:      use CGI qw [ -nph :standard ]; The '-nph' triggers a header equivalent to the aforementioned 'nph-' prefix on the CGI. There are several alternative methods, plus other discussion, in the CGI documentation under 'NPH'.

      Of course, you will have to unbuffer STDOUT for full effect, as with:      $|++; As a further note, make sure the HTML you are sending to the browser can be displayed immediately. Under Netscape, for example, a table will not display until it is fully loaded. If you keep sending table data ad infinitum, the table will never display. This also applies to other tags including, but not limited to, UL and OL.
Re: how to implement "tail -f" using CGI
by Starky (Chaplain) on May 17, 2001 at 04:47 UTC
    In the cheezy but effective category, if you are trying to create the visual impression of a continuously updated document but don't want the web server to have a bunch of processes loitering for extended periods of time sucking up system resources, you can use a meta tag like
    <meta HTTP-EQUIV="REFRESH" CONTENT="15;URL=http://www.myurl.com/myapp?tail=mytail">
    
    If the URL is self-referencing, then the page continuously updates itself every 15 seconds. This strategy is used by the Big Brother monitoring tool, for example. Hope this helps!
Re: how to implement
by Anonymous Monk on May 16, 2001 at 23:50 UTC
    Reword it as "How to turn off the Buffering in the Browser"! And the Solution should be clear.
Re: how to implement
by poomba (Initiate) on May 16, 2001 at 23:47 UTC
    doh! original subject should have been "how to implement "tail -f" using CGI".

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-19 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found