safe simultaneous file append?

Paul Rubin phr-n2001d at nightsong.com
Mon Oct 8 14:13:36 EDT 2001


"Chris Gonnerman" <chris.gonnerman at newcenturycomputers.net> writes:
> The only thing you need to do for safety is to ensure that complete
> messages are written by a single write(2) call.  Do this:
> 
>     logfile.write("A long message...\n")

I don't think that guarantees a single write(2) call.  It writes with
stdio and buffers the message, and if the buffered stuff exceeds BUFSIZ,
it writes out BUFSIZ characters and saves the rest.  So the message
can get chopped across two or more write(2) calls.

Flushing the logfile object after every write should cause a single
write call AS LONG AS every message is <= BUFSIZ chars (512 bytes
in the old days, 8k on my current Linux box).

It's best to use os.write, as someone mentioned.



More information about the Python-list mailing list