Are file.write
operations atomic in Python or C?
Consider the following two threads
with open('foo', 'a') as f:
f.write('123456')
with open('foo', 'a') as f:
f.write('abcdef')
Are we guaranteed not to get intermingled text like the following?
1a2b3c4d5e6f
or
123abc456def
but instead get one of the two possible correct results
123456abcdef
abcdef123456
Note that there is a single call to write in each thread, obviously atomic multiple writes would require some sort of lock. I'm also aware of file-based locks. The ideal answer to this question is yes/no along with evidence/documentation.
It looks like the underlying OS write() call might not even be atomic:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.