import os import threading NUM_THREADS = 10 NUM_WRITES = 100 WRITE_LEN = 1000 path = 'out.txt' open(path, 'w').close() def thr(i): b = bytes((ord('a') + i,)) with open(path, 'ab', buffering=0) as out: for _ in range(NUM_WRITES): out.write(b * WRITE_LEN) ts = [] for i in range(NUM_THREADS): t = threading.Thread(target=thr, args=(i,)) t.start() ts.append(t) for t in ts: t.join() with open(path, 'rb') as inp: size = os.fstat(inp.fileno()).st_size if size != NUM_THREADS * NUM_WRITES * WRITE_LEN: print('Wrong size:', size) else: for _ in range(NUM_THREADS * NUM_WRITES): b = inp.read(WRITE_LEN) if b != b[:1] * WRITE_LEN: print('Mixed data:', b) break else: print('OK')