Is Python file.write atomic?

Are file.write atoms in Python or C?

Example

Consider the following two threads

Theme 1

 with open('foo', 'a') as f: f.write('123456') 

Theme 2

 with open('foo', 'a') as f: f.write('abcdef') 

Are we guaranteed that we do not get mixed text as follows?

 1a2b3c4d5e6f or 123abc456def 

but instead get one of two possible correct results

 123456abcdef abcdef123456 

Note that there is one call to write in each thread, obviously, atomic multiple writes require some locking. I also know file locks. The ideal answer to this question is yes / no, as well as evidence / documentation.

+5
source share
1 answer

It seems that the main call to write () of the operating system may not even be atomic:

Atomicity of `write (2)` in the local file system

+2
source

All Articles