1 file, multiple threads

Nick Coghlan ncoghlan at email.com
Sat Nov 27 08:19:05 EST 2004


Jeff Shannon wrote:
> 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.

Yes - Aahz's somewhat terse suggestion of using the threading module and a Queue 
is the best way to achieve this.

Localising everything to do with parsing and formatting the file correctly also 
becomes trivial in this case - the clients of the file handler thread can deal 
with standard Python objects, leaving the file handler to cope with the file 
format issues.

It can also be very fast, if the file is small enough that the whole thing can 
be loaded into memory.

Regards,
Nick.



More information about the Python-list mailing list