0
import threading


def read_file():
  f = open('text.txt')
  for line in f:
      print line.strip() ,' : ',  threading.current_thread().getName()

if __name__ == '__main__':
  threads = []
  for i in range(15):
    t = threading.Thread(target=read_file)
    threads.append(t)
    t.start()

Question: Will each thread read each line only once from the file above or there are chances that a given thread can end up reading a line twice?

My understanding was that a thread started later will overwrite the file handle for the thread started earlier causing the earlier thread to end up reading few lines twice or thrice or more times.

When I ran this code the outcome was different from what I expected to happen.

Any explanations are welcome.

1
  • 1
    What was the outcome when you ran the program? Did you notice any demons flying out of your nose? :) Commented Oct 8, 2016 at 22:41

1 Answer 1

9
+50

Each thread runs your function independently; each copy of the function opens the file as a local, which is not shared. Each Python file object tracks reading state completely independently; each has their own OS-level file handle here.

So no, if nothing else is altering the file contents, each thread will see each line just once, just the same as if separate processes tried to read the file.

Sign up to request clarification or add additional context in comments.

2 Comments

But why does each thread run the function independently ? It's the same function object being passed to each thread. So how can each thread open the file as a local ?
@abc: yes, you are passing in a function object. So a thread is started and on that thread the function is actually called. When a function is called it creates a new frame with their own locals structure. Each thread calls the function separately, each thread gets their own function frame and each function frame has locals. Those locals are entirely independent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.