fcntl problems
Miles
semanticist at gmail.com
Fri Aug 31 02:10:45 EDT 2007
On 8/30/07, mhearne808 wrote:
> I'm having a number of problems with the fcntl module.
Read this first: http://linux.die.net/man/2/flock
> 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
That should be:
>>> fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
> 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!
This is due to two issues: caching and file position. When you open
the file in both processes as 'w+', they are both positioned at the
*current* EOF, but from that point on the offset is not externally
influenced. The correct sequence of events should be:
- open file in mode w+
- obtain exclusive lock
- f.seek(0, 2) # (to end of file)
- write to file
- f.flush() # or f.close()
- release lock
> Is this my lack of understanding, or have I discovered a bug?
If you find yourself asking this question, it's too often the former :)
-Miles
More information about the Python-list
mailing list