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

Roy Smith roy at panix.com
Fri May 27 09:14:25 EDT 2005


gabor <gabor at nekomancer.net> wrote:
> so, how does one synchronizes several processes in python?

This is a very hard problem to solve in the general case, and the answer 
depends more on the operating system you're running on than on the 
programming language you're using.

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.

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.



More information about the Python-list mailing list