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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With