467,169 Members | 1,059 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,169 developers. It's quick & easy.

[fcntl]how to lock a file

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
Apr 6 '06 #1
  • viewed: 11823
Share:
5 Replies
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


http://aspn.activestate.com/ASPN/Coo...n/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.

Apr 7 '06 #2
thank you a lot for your answering
Bye
Marcello
Apr 10 '06 #3
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
cv*********@mvista.com
Build and Release
MontaVista Software

Apr 10 '06 #4
Carl J. Van Arsdall wrote:
[...]

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

Apr 11 '06 #5
In article <e1**********@emma.aioe.org>, marcello <ma*******@gmail.com>
wrote:
Carl J. Van Arsdall wrote:
> [...]
>
> 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?


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, do**@u.washington.edu
Apr 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by darkhorse | last post: by
2 posts views Thread by nospam | last post: by
4 posts views Thread by KDB | last post: by
12 posts views Thread by Elmo Mäntynen | last post: by
15 posts views Thread by Amir Michail | last post: by
9 posts views Thread by mhearne808[insert-at-sign-here]gmail[insert-dot-he | last post: by
5 posts views Thread by kkadrese@gmail.com | last post: by
2 posts views Thread by xucs007@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.