What would happen if a same file being read and appended at the same time(python programming)?

Posted by Shane on Stack Overflow See other posts from Stack Overflow or by Shane
Published on 2011-01-10T11:37:18Z Indexed on 2011/01/10 11:53 UTC
Read the original article Hit count: 183

I'm writing a script using two separate thread one doing file reading operation and the other doing appending, both threads run fairly frequently.

My question is, if one thread happens to read the file while the other is just in the middle of appending strings such as "This is a test" into this file, what would happen?

I know if you are appending a smaller-than-buffer string, no matter how frequently you read the file in other threads, there would never be incomplete line such as "This i" appearing in your read file, I mean the os would either do: append "This is a test" -> read info from the file; or: read info from the file -> append "This is a test" to the file; and such would never happen: append "This i" -> read info from the file -> append "s a test".

But if "This is a test" is big enough(assuming it's a bigger-than-buffer string), the os can't do appending job in one operation, so the appending job would be divided into two: first append "This i" to the file, then append "s a test", so in this kind of situation if I happen to read the file in the middle of the whole appending operation, would I get such result: append "This i" -> read info from the file -> append "s a test", which means I might read a file that includes an incomplete string?

© Stack Overflow or respective owner

Related posts about python

Related posts about multithreading