[fcntl]how to lock a file

Carl J. Van Arsdall cvanarsdall at mvista.com
Mon Apr 10 13:51:40 EDT 2006


You know, I spent some time with this module and had some interesting 
results.  I had to do file locking over nfs using lockd.  This poised an 
interested problem as I couldn't get the python fcntl module to work 
correctly via nfs even with lockd setup.  Anyhow, I ended up writing an 
extention in C and somehow this called fcntl differently and I had no 
issues.

If you end up having problems working with the python fcntl module let 
me know your configuration I'd be interested to see if anyone else had 
similar problems to me.

-c

marcello wrote:
> Hello
> I need to do this:
> 1 opening a file for writing/appending
> 2 to lock  the file as for writing (i mean: the program
> that lock can  keep writing, all others programs can't )
> 3 wtite and close/unlock
> Even better would be changing the order of steps   1 and 2 (that 
> is,first locking than writing, but it seems to me that in order to block 
> i need the id of the file...)
>
> Untill now i have tried to do smthing like :
> import fcntl
> f=open("prova.txt", 'w')
> fcntl.flock(f.fileno(),fcntl.LOCK_EX)
> f.write("aaa")
>
> #meanwhile i open prova.txt with an editor in another window
> #and write in  "bbb" (without getting any error/warning)and then follow 
> #in the python shell with
>
> f.close()
>
> But then opening the file i can see "bbb" instead of "aaa"
> I'm wandering, am i completely lost(misled?)Am i waiting for fcntl to do 
> what it doesn't?
>
> I have tried too with:
>
> f1=os.open('prova.txt',os.O_EXCL|os.O_APPEND)
> os.write(f1,"qlcosa")
> that should solve the problem of the order of the step, but i not even 
> can write from the python shell with that.
>
> python 2.2
> linux
> kernel 2.4
> glibc-2.3.2-95.6
>
> Thanks in advance for any help.
> sorry for my bad english
> Grazie
> Marcello
>   


-- 

Carl J. Van Arsdall
cvanarsdall at mvista.com
Build and Release
MontaVista Software




More information about the Python-list mailing list