fcntl problems

mhearne808[insert-at-sign-here]gmail[insert-dot-here]com mhearne808 at gmail.com
Fri Aug 31 01:18:09 CEST 2007


On Aug 30, 4:19 pm, "mhearne808[insert-at-sign-here]gmail[insert-dot-
here]com" <mhearne... at gmail.com> wrote:
> I'm having a number of problems with the fcntl module.  First off, my
> system info:
>
> Mac OS X
> Darwin igskcicglthearn.cr.usgs.gov 8.10.1 Darwin Kernel Version
> 8.10.1: Wed May 23 16:33:00 PDT 2007; root:xnu-792.22.5~1/RELEASE_I386
> i386 i386
> Python 2.5.1 (built from source)
>
> OK, the weirdness:
>
> First of all, if I try this:
> file = open("counter.txt","w+")
> fcntl.flock(file.fileno(), fcntl.LOCK_NB)
>
> I get this:
> ---------------------------------------------------------------------------
> <type 'exceptions.IOError'>               Traceback (most recent call
> last)
> /Users/mhearne/src/python/<ipython console> in <module>()
> <type 'exceptions.IOError'>: [Errno 9] Bad file descriptor
>
> However, if I try this:
> fcntl.flock(file.fileno(), fcntl.LOCK_EX)
>
> I get no errors.
>
> Proceeding forward with the locked file, let's say I do the above in
> Python interactive Process A.  Then in python interactive Process B, I
> repeat the "open" function on the same file with the same
> permissions.  Then, in each process, I write some text to the file
> using the write() method.  After closing the file in both processes,
> the only text I see in the file is from Process B!
>
> According to my Python Cookbook:
> "Exclusive lock: This denies all _other_ processes both read and write
> access to the file."
>
> I seem to be experiencing the reverse of that description.  Is this my
> lack of understanding, or have I discovered a bug?
>
> Thanks,
>
> Mike

I've been doing some experiments, and here are some specific examples
to try.  I'll designate the two interactive python processes as PA and
PB.  Both processes were started in the same directory.  Here goes:
PA: import fcntl
PA: f = open("foo.txt","w+")
PA: fcntl.flock(f.fileno(),fcntl.LOCK_EX)
PA: f.write("text1\n")
PB: f = open("foo.txt","w+")
PB: f.write("text2\n")
PA: f.close()
PB: f.close()

contents of foo.txt are:
text2

Second experiment:

PA: f = open("foo.txt","w+")
PA: fcntl.flock(f.fileno(),fcntl.LOCK_EX)
PB: f = open("foo.txt","w+")
PA: f.write("text1\n")
PB: f.write("text2\n")
PA: f.write("text3\n")
PB: f.close()
PA: f.write("text4\n")
PA: f.close()

contents of foo.txt are:
text1
text3
text4

Third experiment:
PA: f = open("foo.txt","w+")
PA: fcntl.flock(f.fileno(),fcntl.LOCK_EX)
PA: f.write("text1\n")
PB: f = open("foo.txt","w+")
PB: f.write("text2\n")
PB: f.close()
PA: f.close()

contents of foo.txt are:
text1

Fourth experiment:
PA: f = open("foo.txt","w+")
PA: f.write("text1\n")
PB: f = open("foo.txt","w+")
PB: f.write("text2\n")
PB: f.close()
PA: f.close()

contents of foo.txt are:
text1

>From these last two experiments I can only conclude that file locking
isn't doing a durned thing.

What's going on?

--Mike




More information about the Python-list mailing list