Force flushing buffers

Bengt Richter bokr at oz.net
Sat Oct 15 19:56:19 EDT 2005


On Wed, 12 Oct 2005 15:55:10 -0400, Madhusudan Singh <spammers-go-here at spam.invalid> wrote:

>Robert Wierschke wrote:
>
>> Madhusudan Singh schrieb:
>>> Hi
>>> 
>>>         I have a python application that writes a lot of data to a bunch
>>>         of files
>>> from inside a loop. Sometimes, the application has to be interrupted and
>>> I find that a lot of data has not yet been writen (and hence is lost).
>>> How do I flush the buffer and force python to write the buffers to the
>>> files ? I intend to put this inside the loop.
>>> 
>>> Thanks.
>> disable the buffer!
>> 
>> open( filename[, mode[, bufsize]])
>> 
>> open takes an optional 3ed argument set bufsize = 0 means unbuffered.
>> see the documentation of the in build file() mehtod as open is just
>> another name
>
>Though I will not be using this solution (plan to use flush() explicitly)
>for speed reasons, thanks ! I will file this away for future reference :)
I suspect Scott's try/finally approach will get you better speed, since it
avoids unneeded flush calls and the associated buffer management, but
it is best to measure speed when you are concerned.

Regards,
Bengt Richter



More information about the Python-list mailing list