Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: STDOUT flush, Was: Simple requests using LWP::UserAgent

by baku (Scribe)
on Dec 06, 2001 at 02:32 UTC ( #129780=note: print w/ replies, xml ) Need Help??


in reply to Simple requests using LWP::UserAgent

The STDOUT is a buffered handle. Buffered I/O is the typical on Unix systems - it saves the system time from having to start I/O, perform I/O, and stop I/O as often, by waiting for a certain amount of I/O to be "ready" before doing it. Imagine, for example, if your printer was warmed up, told to position the print head, write a single character, then told to reset the print head, every time you did a print LPR 'x' - a very bad thing. On a smaller scale, this applies also to disc I/O, network communications, &c. - there's almost always a "cost" to starting and stopping I/O.

However, when you're working with a "tty," it's pretty unlikely to be an actual TeleType(TM) these days. What I tend to do, is add a chunk like this up front:

use FileHandle; # Maybe should be File::IO ? if ( -t STDOUT ) { STDOUT->autoflush(1); } # OR: without FileHandle, save some RAM at the expense # of legibility: # braces keep "my" vars local here. { # Save the currently select:ed filehandle! my $saved_selected_fh = select STDOUT; if ( -t ) { ++$|; # turn on "autoflush" } }

N.B. that your "flush" code is not a flush (see  perldoc on  flush ) -- it's actually toggling on the autoflush mode on STDOUT. That's a fancy way of saying that Perl will automatically  flush the STDOUT buffer - i.e. tell the OS to do the I/O right now, don't wait for more data - every time you do a  print or other write to it.

Since TTY or disc I/O is so fast, I'd recommend just sticking a  ++$| (turn on autoflush on the currently-selected output channel, which is STDOUT unless you've done a  select ; the current channel is the one that  print LIST will print to, as opposed to  print FILEHANDLE LIST ) at the beginning, rather than re-enabling autoflush for each line of data. It's not a big performance hit, but it is a bit of overkill.

If you really do want to manually control when the buffer is flushed (barring Unix getting all smart and surprising you by doing it first ... it can if it wants to ), just put a  flush STDOUT; in there.

Just for reference, on most unices the buffer for a tty is about 4Kbytes. If you changed a  print to produce, say, 4097 (4 * 1024 + 1) bytes of output, it would probably trigger a flush right off...


Comment on Re: STDOUT flush, Was: Simple requests using LWP::UserAgent
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-08-29 06:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (276 votes), past polls