write to the same file from multiple processes at the same time?
gabor
gabor at nekomancer.net
Mon May 30 08:28:11 EDT 2005
gabor wrote:
> Jp Calderone wrote:
>
>> To briefly re-summarize, when you want to acquire a lock, attempt to
>> create a directory with a well-known name. When you are done with it,
>> delete the directory. This works across all platforms and filesystems
>> likely to be encountered by a Python program.
>
>
> thanks...
>
> but the problem now is that the cgi will have to wait for that directory
> to be gone, when he is invoked.. and i do not want to code that :)
> i'm too lazy..
>
> so basically i want the code to TRY to write to the file, and WAIT if it
> is opened for write right now...
>
> something like a mutex-synchronized block of the code...
>
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..
gabor
More information about the Python-list
mailing list