'Python appending to a file concurrently

I wrote a simple program in Python:

from random import random
from threading import Thread
from time import sleep

def write_to_file(i):
    sleep(random())
    with open("test.txt", "a") as f:
        f.write(f"{i}\n")

for i in range(20):
    Thread(target=write_to_file, args=[i]).run()

I'd expect this program to write numbers from 0-19 to test.txt in random order. Instead, the program writes 0-19 in order, and it takes a few seconds to complete, signifying that it executed threads one after the other, in order.

How does this work? Does with open(...) block every other thread from running that also has with open(...) on the same file?



Solution 1:[1]

Does with open(...) block every other thread from running that also has with open(...) on the same file?

No, it doesn't.

run method of a thread starts and joins the thread which means that it waits for a thread to be joint for every iteration. To make it parallel you must call start instead of run then join all threads at once.

from random import random
from threading import Thread
from time import sleep


def write_to_file(i):
    sleep(random())
    with open("test.txt", "a") as f:
        f.write(f"{i}\n")


threads = []

for i in range(20):
    thread = Thread(target=write_to_file, args=[i])
    thread.start()
    threads.append(thread)

for thread in threads:
    thread.join()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Artyom Vancyan