When you go to board a plane at Heathrow, do the ground staff admit the passengers one at a time, ensuring that each is seated before the next boards, and sealing and re-opening the main doors between each?
What a stupid and inefficient system that would be!!
The same thing applies when you're programming - outputs that you THINK that you're printing directly to screen, to file, to pipe, or (via a socket) to a process on another machine may in fact be waiting in the lounge - buffered - to be output with greater efficiency in a single block.
Should you wish to read from a newly written resource, then, you
might have to wait. In the majority of cases it would not be a problem. Language libraries are usually built with a number of checkpoints at which data is "flushed" even if buffers aren't full. Such flushes commonly occur:
a) When a new line is generated (keep screen up to date)
b) When an input is about to be read (so that prompts can be seen!)
c) When the buffer reaches 4k of data
d) When the file handle is closed
e) When the program exits
and these account for most, but not all, of the cases the programmer needs to be aware of.
"But not all" I wrote. Here are some common exceptions:
In
Tcl, the
fconfigure command gives you channel by channel control. By default, flushing happens line by line (and so NOT when you're about to do a read) and this means you'll want to
flush stdout after outputting a prompt with
puts -nonewline. Otherwise, the prompt is only issued on the screen AFTER you've answered the question.
[example]
In
Perl, output is similarly flushed at new line, and in addition it is flushed at read. Good - no need for Tcl's flush. However, if you're generating a progress bar - either a series of dots or a line that's being overwritten time and again, then you'll need to flush. Have a look at
autoflush and have a look at the special variable
$| (that's dollar-pipe). Neither of these flushes itself - rather they add an extra flush at the end of each print. See
[/example].
Python has a similar arrangement to Perl, so it's quite rare that you need to flush the screen output - but again you DO need to do so with a progress bar.
[example].
I describe
PHP as being a "batch" or "batchlet" language - at least in its intended web use. So it doesn't have the interactive buffering issues associated with keyboard-to-screen in most uses. Actually, you have rather the opposite problem sometimes. In PHP, if you try to call the
header function, or anything else that could effect the headers (even
session_start can do it) after you have generated some output, you'll get an error message like
Warning: Cannot modify header information - headers already sent by .... There *is* an
ob_start function to turn ON output buffering but, really, best programming practise is to generate your headers early!
There are PHP examples broken
here and working
here. They include links to source.
(written 2007-04-06, updated 2009-08-31)
Associated topics are indexed under
G997 - Well House Consultants - Newsletter Lead Articles [3202] Telling you something about us in just one line - (2011-03-15)
[2743] Public Open Source Training Courses running this summer and autumn in Melksham - (2010-04-27)
[2538] Open Source Training Centre and Courses for 2010 - (2009-12-16)
[2425] Weekend and Christmas Promotion - Well House Manor Hotel, Melksham - (2009-09-26)
[2370] C++, Python, and other training - do we use an IDE - (2009-08-21)
[2253] Walks in and around Melksham, Wiltshire - (2009-06-21)
[2119] Make your business a DESTINATION business - (2009-04-05)
[2052] How was my web site compromised? - (2009-02-24)
[1912] Book now for 2009 - (2008-11-29)
[1819] Calling base class constructors - (2008-10-03)
[1754] Upgrade from PHP 4 to PHP 5 - the TRY issue - (2008-08-15)
[1663] Python in an afternoon - a lecture for experienced programmers - (2008-06-01)
[1600] Cambidge - Tcl, Expect and Perl courses - (2008-04-04)
[1545] Letting new visitors know we provide training courses - (2008-02-19)
[1488] New trainee laptop fleet for our Open Source courses - (2007-12-30)
[1386] New software product for warmblooded programmers - (2007-10-10)
[1318] Well House Manor - feature comparison against the old place! - (2007-08-24)
[1224] Object Relation Mapping (ORM) - (2007-06-09)
[1065] Graham Ellis - an Introduction - (2007-02-05)
[1000] One Thousand Posts and still going strong - (2006-12-18)
H110 - PHP - HTML Web Page Data Handling [3036] Sending out an email containing HTML from within a PHP page - (2010-11-07)
[2135] What features does this visitors browser support? (PHP) - (2009-04-22)
[2107] How to tweet automatically from a blog - (2009-03-28)
[2046] Finding variations on a surname - (2009-02-17)
[2025] Injection Attack if register_globals in on - PHP - (2009-02-04)
[1831] Text formating for HTML, with PHP - (2008-10-11)
[1169] Emailing as HTML (Web Page) - PHP example - (2007-04-30)
[1053] Sorting people by name in PHP - (2007-01-26)
[1001] .pdf files - upload via PHP, store in MySQL, retrieve - (2006-12-19)
[896] PHP - good coding practise and sticky radio buttons - (2006-10-17)
[789] Hot answers in PHP - (2006-07-02)
[589] Robust PHP user inputs - (2006-02-03)
[50] Current cost in your local currency - (2004-09-16)
P210 - Perl - Topicalization and Special Variables [3449] Apache Internal Dummy Connection - what is it and what should I do with it? - (2011-09-19)
[2972] Some more advanced Perl examples from a recent course - (2010-09-27)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[1922] Flurinci knows Raby Lae PHP and Jeve - (2008-12-04)
[1860] Seven new intermediate Perl examples - (2008-10-30)
[1829] Dont bother to write a Perl program - (2008-10-10)
[1728] A short Perl example - (2008-07-30)
[1705] Environment variables in Perl / use Env - (2008-07-11)
[1704] Finding operating system settings in Perl - (2008-07-10)
[1508] How not to write Perl? - (2008-01-15)
[1444] Using English can slow you right down! - (2007-11-25)
[1289] Pure Perl - (2007-08-03)
[1232] Bathtub example - (2007-06-14)
[1221] Bathtubs and pecking birds - (2007-06-07)
[969] Perl - $_ and @_ - (2006-12-07)
[639] Progress bars and other dynamic reports - (2006-03-09)
[493] Running a Perl script within a PHP page - (2005-11-12)
T202 - Tcl/Tk - Tcl Fundamentals [2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
[1469] Curley brackets v double quotes - Tcl, Tk, Expect - (2007-12-12)
[1426] Buffering up in Tcl - the empty coke can comparison - (2007-11-10)
[1282] Stringing together Tcl scripts - (2007-07-29)
[782] Converting between Hex and Decimal in Tcl - (2006-06-28)
[362] The ireallyreallywanna operator - (2005-06-28)
[349] Comments in Tcl - (2005-06-16)
[328] Making programs easy for any user to start - (2005-05-29)
[210] Joining lists in Tcl. Indirect variables in Tcl. - (2005-02-12)
[3] Looking for a donkey - (2004-08-05)
Y115 - Additional Python Facilities [3469] Teaching dilemma - old tricks and techniques, or recent enhancements? - (2011-10-08)
[3442] A demonstration of how many Python facilities work together - (2011-09-16)
[3089] Python regular expressions - repeating, splitting, lookahead and lookbehind - (2010-12-17)
[2790] Joining a MySQL table from within a Python program - (2010-06-02)
[2786] Factory methods and SqLite in use in a Python teaching example - (2010-05-29)
[2765] Running operating system commands from your Python program - (2010-05-14)
[2764] Python decorators - your own, staticmethod and classmethod - (2010-05-14)
[2746] Model - View - Controller demo, Sqlite - Python 3 - Qt4 - (2010-04-29)
[2745] Connecting Python to sqlite and MySQL databases - (2010-04-28)
[2721] Regular Expressions in Python - (2010-04-14)
[2655] Python - what is going on around me? - (2010-02-28)
[2462] Python - how it saves on compile time - (2009-10-20)
[2435] Serialization - storing and reloading objects - (2009-10-04)
[2407] Testing code in Python - doctest, unittest and others - (2009-09-16)
[1876] Python Regular Expressions - (2008-11-08)
[1337] A series of tyre damages - (2007-09-08)
[1336] Ignore case in Regular Expression - (2007-09-08)
[1305] Regular expressions made easy - building from components - (2007-08-16)
[1149] Turning objects into something you can store - Pickling (Python) - (2007-04-15)
[1043] Sending an email from Python - (2007-01-18)
[901] Python - listing out the contents of all variables - (2006-10-21)
[753] Python 3000 - the next generation - (2006-06-09)
[672] Keeping your regular expressions simple - (2006-04-05)
[663] Python to MySQL - (2006-03-31)
[463] Splitting the difference - (2005-10-13)
[239] What and why for the epoch - (2005-03-08)
[208] Examples - Gadfly, NI Number, and Tcl to C interface - (2005-02-10)
[183] The elegance of Python - (2005-01-19)
Some other Articles
Python GTK - Widget, Packing, Event and Feedback exampleOvercrowded trains around BristolThe Holiday - unlikely romantic comedy?Wiltshire CirclesBuffering output - why it is done and issues raised in Tcl, Perl, Python and PHPGovernment pressure on me to shut up after I make the papersFunction / method parameters with * and ** in PythonB-2-B Networking, 20th April. Useful for YOUR business?Well House Manor, Melksham, Art GalleryMySQL - Password security (authentication protocol)