write to the same file from multiple processes at the same time?

Peter Hansen peter at engcorp.com
Fri May 27 09:18:17 EDT 2005


Roy Smith wrote:
> gabor <gabor at nekomancer.net> wrote:
> On the other hand, you said that each process will be writing a single line 
> of output at a time.  If you call flush() after each message is written, 
> that should be enough to ensure that the each line gets written in a single 
> write system call, which in turn should be good enough to ensure that 
> individual lines of output are not scrambled in the log file.

Unfortunately this assumes that the open() call will always succeed, 
when in fact it is likely to fail sometimes when another file has 
already opened the file but not yet completed writing to it, AFAIK.

> If you want to do better than that, you need to delve into OS-specific 
> things like the flock function in the fcntl module on unix.

The OP was probably on the right track when he suggested that things 
like SQLite (conveniently wrapped with PySQLite) had already solved this 
problem.

-Peter



More information about the Python-list mailing list