Buffering control in python?
James J. Besemer
jb at cascade-sys.com
Sat Oct 12 21:58:09 EDT 2002
Fernando Pérez wrote:
>I know that using -u will turn buffering off for everything in python. But is
>there a good reason for the lack of a way to turn buffering off from _inside_
>a script?
>
>In Perl each stream can be set to unbuffered via a simple
>
>STDOUT->autoflush(1);
>
The optional 3rd argument to the open() function( file() in 2.2)
controls buffering. The doc says it controls the buffer size (which it
does) but the fine print also says a value of 1 results in "line"
buffering and a value of 0 results in unbuffered. I guess you have to
close and reopen stdin or stdout if they're the streams you wish to
unbuffer. Unless there's a better way I haven't discovered. Anybody?
If you're interested specifically in serial I/O, there was a post here
in just the last week or so about how to do non-blocking I/O for serial
ports on unix/Linux machines. However, it had more to do with telling
the OS to not buffer I/O than the file buffers. The trick was using the
posix module to effect the appropriate stty changes.
In the remote chance you are interested in implementing some kind of
"log" file, and want the file to accurately reflect the latest status,
then I suggest foregoing buffering altogether. Instead, there is an
advantage in biting the bullet and actually opening and closing the file
for each append mode write. This way, you or automated utilities can
"rotate" the log file (rename it, compress it, etc.) with impunity.
Otherwise the file will be locked by your daemon and you can't "rotate"
without also shutting down the logging service.
Regards
--jb
--
James J. Besemer 503-280-0838 voice
2727 NE Skidmore St. 503-280-0375 fax
Portland, Oregon 97211-6557 mailto:jb at cascade-sys.com
http://cascade-sys.com
More information about the Python-list
mailing list