1 file, multiple threads

Jeff Shannon jeff at ccvcorp.com
Fri Nov 26 18:43:06 EST 2004


Jason wrote:

>>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. 
>  
>

You *can*, but due to buffering issues, it's likely that the reader 
threads will not see changes made by the writer thread properly, and may 
have issues with separate disk-reads which nominally stop/start at the 
same location not actually matching because the underlying disk file has 
changed.  (Note that disk reads, which go into a buffer, do not 
necessarily correlate in any predictable way to calls to the read*() 
family of functions.)

In order to ensure consistent access to a mutable (i.e. not read-only) 
file from multiple threads, it would be necessary to ensure that only 
one thread was accessing the file at a given instant (i.e. use some form 
of locking/synchronizing mechanism), and to be careful to flush all 
buffers both before and after any file access.  If the file can change 
at all, then the only time that a given thread can make *any* 
assumptions about the state of the file is during a single section in 
which that thread has exclusive access to the file.

Much simpler to designate a single file-handler 'server', and have each 
thread access the file only through the intermediary of this server 
(which could be implemented as a separate thread itself).  The server 
then manages all of the file buffers, both in and out, and can ensure 
that each access happens in a consistent way.

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list