title: Are multi-process appends to a file atomic in Python/Linux?
body: <p>I have a bunch of processes saving their partial progress to same file during a long-running program.</p>
  <p>A way of doing this safely is using a <code>multiprocessing.Lock</code>.</p>
  <p>But I wonder if simply <strong>appending</strong> to the file is also safe?</p>
  <p>It only matters that each <strong>write itself</strong> is atomic. But the writes can be in any order (each write has data about the process that wrote it).</p>
  <p>The following example seems to be working as intended...</p>
  <pre><code>import multiprocessing as mp
  import time
  import random
  import os
  
  # ----------------------------------------------------------------
  def thr_work00(args):
    arg0 = args[0]
    arg1 = args[1]
  
    for i in range(0x3):
      s = random.random()/10
      time.sleep(s)
      print(f'\x1b[92m{arg0} \x1b[32m{s:.3f}\x1b[0m')
      with open('test.txt','a') as o_file:
        o_file.write(f'{180*str(arg0)}{arg1:02x}\n')
  
    return [arg0,s]
  
  # ----------------------------------------------------------------
  if os.path.exists('test.txt'):
    os.remove('test.txt')
  
  tasks = [
    [0, 0xf0],
    [1, 0xf1],
    [2, 0xf2],
    [3, 0xf3],
    [4, 0xf4],
    [5, 0xf5],
    [6, 0xf6],
    [7, 0xf7],
  ]
  
  with mp.Pool(0x4) as pool:
    results = pool.map(thr_work00,tasks)
    print()
    for res in results: print(res)
  </code></pre>