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

Mike Meyer mwm at mired.org
Mon May 30 14:12:49 EDT 2005


gabor <gabor at nekomancer.net> writes:

> ok, i ended up with the following code:
>
> def syncLog(filename,text):
>      f = os.open(filename,os.O_WRONLY | os.O_APPEND)
>      fcntl.flock(f,fcntl.LOCK_EX)
>      os.write(f,text)
>      #FIXME: what about releasing the lock?
>      os.close(f)
>
> it seems to do what i need ( the flock() call waits until he can get
> access).. i just don't know if i have to unlock() the file before i
> close it..

The lock should free when you close the file descriptor. Personally,
I'm a great believer in doing things explicitly rather than
implicitly, and would add the extra fcntl.flock(f, fcntl.LOCK_UN) call
before closing the file.

       <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list