Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

cgi buffer flush

by arc_of_descent (Hermit)
on Mar 09, 2002 at 10:20 UTC ( [id://150524]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, This is my first post, so plz excuse any noviceness that i may display!
I've been cracking my head against output buffering in cgi.
I'm using mod_perl with apache 1.3.22
Here is what i'm trying to do:

#!/usr/bin/perl # make output filehandle HOT $| = 1; print "Content-Type: text/html\r\n\r\n"; print "Sleeping for 10 seconds<br>\n"; sleep 10; print "Wide Awake Now.<br>\n";


Now what i expect in the browser is that the "Sleeping" string should be output and then 10 seconds later, the "Wide Awake" string should appear.
But unfortunately, both appear together after 10 seconds.
How do i seperate the two?
I basically want this for better html responses, as i do a lot of processing in my cgi and i want the user to know whats going on behind the scenes.
Does it have something to do woth apache buffering?
Thanx.

--
arc_of_descent

Replies are listed 'Best First'.
Re: cgi buffer flush
by io (Scribe) on Mar 09, 2002 at 11:10 UTC
    note the "\r\n\r\n" can be safely written as "\n\n" as perl will convert them when you are on windows. Thus making your code more portable. to flush to buffer, just put $|++ in the head of your script. Offcourse, if you really want the text to appear after 10 seconds i'd use javascript.
      Isn't "$|++" the same as "$|=1"?

      -fuzzyping
        Isn't "$|++" the same as "$|=1"?

        A lot of people seem to think it is, but it's not.

        • $|++ adds one to the current value of $|.
        • $| = 1 sets the value of $| equal to one.
        If for some reason $| = -1, doing a $|++ will make $| = 0, and usually that is not what $|++ is meant to do.

        $| = 1 is much safer and more straight forward (less ambiguous?).

        Update: Whoops. I was way wrong. A thousand lashes to me for misleading my fellows due to my neglectful ignorance. Thanks to dvergin for the correction and for setting me straight..

        Cheers!

        Brent

        -- Yeah, I'm a Delt.

      "\r\n" is correct in this case. I'm pretty sure it's in the HTTP specs as the official line terminator. I think servers might take "\n", but they don't have to. Telnet is the same way.
Re: cgi buffer flush
by cjf (Parson) on Mar 09, 2002 at 10:48 UTC
    Suffering from Buffering written by Dominus may be of some assistance to you.

    I am also curious about print "Content-type: text/html\r\n\r\n"; - is this essentially the same as print "Content-type: text/html\n\n"; or is there some difference?

    Update: io answered my question with regards to the header++

      Yes i had read that article before and i even read it once more, but unfortunately i don't know what to do next. I have tried all the possoible variations.
      I also did away with print statements and used syswrite instead which does no buffering.
      But still i don't get what i want.
      The script works perfectly on command line

      I'm not sure about the newlines and carriage returns in the Content-Type header.
      My guess is that any of them should do.

      --
      arc_of_descent
Re: cgi buffer flush
by erikharrison (Deacon) on Mar 09, 2002 at 15:52 UTC

    You were clever to make the filehandle hot. It would have been exactly what I would have done to solve this problem. In fact you should always make STDOUT hot in a CGI program to ensure that the headers get to the browser in order.

    However, your problem has to do with two things: Apache behavior and browser behavior. As to the first, I'm no expert at Apache, but I do know that Apache will wait until the program finishes execution before sending anything to the browser - hence the 10 second wait until you see anything. As to the second, you can't be sure when the browser will parse and display the page. It might wait until it gets the whole page, it might parse and display in block - every 8k or so. I don't know. After all the browser doesn't know how tags at the bottom of your page may affect the top. These problems taken to account, it may be obvious that a client side program probably can't solve your problem - you'll need some client side solution - Flash, Curl, or even JavaScript

    Good Luck!

    Cheers,
    Erik
Re: cgi buffer flush
by zentara (Archbishop) on Mar 09, 2002 at 18:17 UTC
    I running apache on linux. Your code works exactly as expected with Netscape 4.79 and mozilla.098 Maybe your browser's cache is redisplaying a cached copy without the delay?
Re: cgi buffer flush
by arc_of_descent (Hermit) on Mar 09, 2002 at 19:50 UTC
    HI,
    Thanx for all u're replies.
    This confirms my earlier doubt that my problem was web server and client buffering issues.
    Which makes me think of the site where i found such behaviour which looked really cool.
    Now i feel they were using some javascript trick, (maybe settimeout) or something similar!
    Maybe i'll use it too!

    Cheers

    --
    arc_of_descent

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-16 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found