Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is iterating over a Python file object thread safe?

While searching for a neat solution to this problem I was wondering if iterating over a Python file object is thread safe or not.

from concurrent.futures import ThreadPoolExecutor
import sys, time

f = open("big_file")

def worker():
    running = True
    while running:
        try:
            line = next(f)
        except StopIteration:
            return
        # process line
        time.sleep(3)
        sys.stdout.write(line + "\n")

no_workers = 4
with ThreadPoolExecutor(max_workers=no_workers) as e:
    for _ in range(no_workers):
        e.submit(worker)

f.close()

My question is, if the example above is safe, or if not, what is an easy way to get a thread-safe file object (for reading a file line by line, writing is not required).

like image 955
tobigue Avatar asked Nov 02 '22 14:11

tobigue


1 Answers

No, file I/O is not thread safe. Locking is a solution, but I think the option of having a single thread deal with each external resource works better. Other threads send work requests to the dedicated thread on a Queue.Queue instance (and provide another queue of their own in case they need result(s) back), the dedicated thread spends most of its time waiting on a .get on that queue and whenever it gets a request it deals with it, possibly returning the results.

like image 100
Michael Foukarakis Avatar answered Nov 15 '22 05:11

Michael Foukarakis