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

gabor gabor at nekomancer.net
Tue May 31 03:54:29 EDT 2005


Mike Meyer wrote:
> 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.

done :)

gabor



More information about the Python-list mailing list