1 file, multiple threads
gaudette at ele.uri.edu
Fri Nov 26 20:39:21 CET 2004
> If I have multiple threads reading from the same file, would that be a
As long as you open each file with 'r' or 'rb' access only, it is not
a problem. I believe you can even write to that file from one (but
only one) thread while reading the file in multiple other threads.
This probably isn't the most robust way and may even cause some
conflicts. You may want to check on this though.
> Let's say I want to take it a step further and start writing to 1 file form
> multiple threads, how would I solve that?
If you use the built-in 'threading' module in Python, you can simple
acquire a mutex (implemented using Semaphore/Lock/RLock) before
writing and release it immediately afterward.
Your main thread would allocate a file lock:
mutex_writefile = threading.Lock()
Each thread writing to a file can do the following:
fh = open(fname, 'a')
It is important that you release your locks or you will have a
deadlock on each thread waiting to write. See the threading module's
documentation for more details.
You could also do the same with the thread module's lock object;
however, in my opinion you gain much more functionality with threading
with little increase in complexity.
More information about the Python-list