1 file, multiple threads

Jason gaudette at ele.uri.edu
Fri Nov 26 14:39:21 EST 2004


> If I have multiple threads reading from the same file, would that be a
> problem?

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.

Example...

Your main thread would allocate a file lock:

mutex_writefile = threading.Lock()

Each thread writing to a file can do the following:

mutex_writefile.acquire()
fh = open(fname, 'a')
fh.write(str)
mutex_writefile.release()

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.

-Jay



More information about the Python-list mailing list