[fcntl]how to lock a file

M

marcello

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
 
E

Eric S. Johansson

marcello said:
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

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203

been using it for years and it makes locking really simple. I have no
idea why the main Python distribution is missing such an important piece
of functionality.
 
C

Carl J. Van Arsdall

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
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
(e-mail address removed)
Build and Release
MontaVista Software
 
M

marcello

Carl said:
> [...]
>
> 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.


Python 2.2.3 (#1, Aug 8 2003, 08:44:02)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-13)] on linux2
kernel 2.4.21-4.0.1.ELsmp
glibc-2.3.2-95.6



What else can i add?

best regards
Marcello
 
D

Donn Cave

[QUOTE="marcello said:
[...]

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.


Python 2.2.3 (#1, Aug 8 2003, 08:44:02)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-13)] on linux2
kernel 2.4.21-4.0.1.ELsmp
glibc-2.3.2-95.6



What else can i add?[/QUOTE]

Maybe something about the problem?

There are four functions in that module - fcntl, flock,
ioctl, lockf. Which one were you using? What were you
trying to do with it?

Did it raise an exception? Returned but didn't effectively
interlock against other processes -- on the same host, on
other hosts? Failed to return when the file should have
been OK to lock?

Is it possible that locks would work fine in a test program,
but fail in your application due to brain damaged POSIX
filesystem locking semantics?

We can start with stuff you probably already know.

- The Berkeley flock(2) function is great but is not
supported by NFS.

- fcntl(2) F_SETLK etc. are supported by NFS, but have
extremely inconvenient semantics, part of the POSIX
1003.1 specifications, particularly the part about losing
a lock on ANY close(), even if the file descriptor remains
open for fcntl(2)'s caller.

- lockf(3) is fcntl(2).

- Some platforms provide an flock() that is actually
fcntl(2), can't think of the right words to express how
deplorable that practice is.

- On the rare platform that doesn't do this - where they
are honest enough to just admit that they don't support
flock() - Python implements fcntl.flock() with fcntl(2),
I guess on the theory that you can't have enough brain
damage. The worst case would be if Python's configure
missed a bona fide flock(2), which is unlikely but may
be worth checking if you use flock(2) for a reason.

Donn Cave, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top