1

Possible Duplicate:
what exactly the python’s file.flush() is doing?

I find that calling the flush method costs many IO operations, which leads to extra time.

I know that if I take out flush in my script, the file will not be updated immediately.

So I wonder if I'm only writing but not reading a file, is there any other side-effect of taking out flush from script?

Community
  • 1
  • 1
ThunderEX
  • 703
  • 1
  • 7
  • 13
  • Read about the effects of flush in the answer to [this question](http://stackoverflow.com/questions/7127075/what-exactly-the-pythons-file-flush-is-doing). If you don't need that in your program, then don't use flush. – Diego Basch Dec 12 '12 at 03:35

1 Answers1

0

Side effects? I dont exactly understand what you mean but let me have ago at it anyway.

For file operations, Python uses the operating system's default buffering unless you configure it do otherwise. You can specify a buffer size, unbuffered, or line buffered. So If you are constantly using flush there is constant IO going on and If you are flushing out large amounts of data (i.e. buffer being big) this could slow does other running programs which could end up waiting for IO.

Fast and frequent IO operations are not good for the life of a harddisk, it increases changes of disk crashes.

Typically the pattern I follow is after all the writing to file object is done, flush is done at the end before closing the file.

Something for you to think about, are there other threads or programs reading from the same file as you are writing it? If this is the case you might get into trouble! Corrupt files are very much a possibility here. If you are considering using file as a persistent data store. Then its the wrong way to do it. Why not consider using a persistent-DB (like mysql or even sqlite) instead of using a file as a data store.

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264