Question: posix fcntl module

Sheila King sheila at spamcop.net
Tue Aug 28 19:17:32 EDT 2001


On Tue, 28 Aug 2001 18:51:40 -0400 (EDT), Ignacio Vazquez-Abrams
<ignacio at openservices.net> wrote in comp.lang.python in article
<mailman.999039163.17806.python-list at python.org>:

:>From the fcntl(2) man page:
:
:"
:       F_SETLK
:              The lock is set (when l_type is F_RDLCK or F_WRLCK)
:              or cleared (when it is F_UNLCK).  If  the  lock  is
:              held by someone else, this call returns -1 and sets
:              errno to EACCES or EAGAIN.
:"
:
:>From <bits/fcntl.h>:
:---
:#define F_RDLCK         0       /* Read lock.  */
:#define F_WRLCK         1       /* Write lock.  */
:#define F_UNLCK         2       /* Remove lock.  */
:---
:
:Those are the values you want to use. It looks like Python may have this one
:wrong.

Well, this sounded good to me. So, I tried this:

''' posixLock.py

class lockfile
supports the same function calls as
winLock.lockfile
export to LockFile.py: a wrapper module
around the win and posix lockfiles
'''

import os, fcntl

class lockfile:
    def __init__(self, filename):
        if os.access(filename, os.F_OK):
            self.filename = filename
        else:
            errmssg = filename + " does not exist. Can't lock non-existent file."
            raise IOError, errmssg

    def getReadLock(self):
        self.f = open(self.filename)
        self.fd = self.f.fileno()
        fcntl.lockf(self.fd, fcntl.F_RDLCK)

    def getWriteLock(self):
        self.f = open(self.filename)
        self.fd = self.f.fileno()
        fcntl.lockf(self.fd, fcntl.F_WRLCK)

    def unlock(self):
        fcntl.lockf(self.fd, fcntl.F_UNLCK)
        self.f.close()
        del self.fd

    def flock(self, flag):
        '''flags are:
        LOCK_UN - unlock
        LOCK_SH - acquire a shared (or read) lock
        LOCK_EX - acquire an exclusive (or write) lock
        '''
        if flag == 'LOCK_SH':
            self.getReadLock()
        elif flag == 'LOCK_EX':
            self.getWriteLock()
        elif flag == 'LOCK_UN':
            self.unlock()
        else:
            errmssg = "The flag " + flag + " is not implemented for flock"
            raise NotImplementedError, errmssg


But, in an interactive session, I got:

Python 2.1.1 (#6, Aug 16 2001, 17:02:08)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> from LockFile import LockFile
>>> obj = LockFile('lock.txt')
>>> obj.flock('LOCK_SH')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "posixLock.py", line 42, in flock
    self.getReadLock()
  File "posixLock.py", line 23, in getReadLock
    fcntl.lockf(self.fd, fcntl.F_RDLCK)
AttributeError: 'fcntl' module has no attribute 'F_RDLCK'
>>>

I suppose I could just use the integer value equivalents, or possibly import the
constants from FCNTL. However, I'm not certain that the integer value equivalents
will be the same across all platforms. 

Hmm. What to do, what to do...?

Well, I tried adding this to the top of my posixLock.py file:

from FCNTL import F_RDLCK, F_WRLCK, F_UNLCK

But then, I just got this error:

Python 2.1.1 (#6, Aug 16 2001, 17:02:08)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> from LockFile import LockFile
>>> obj = LockFile('lock.txt')
>>> obj.flock('LOCK_SH')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "posixLock.py", line 43, in flock
    self.getReadLock()
  File "posixLock.py", line 24, in getReadLock
    fcntl.lockf(self.fd, F_RDLCK)
ValueError: unrecognized flock argument
>>>

Maybe I just need to address the fcntl command, itself (i.e. fcntl.fcntl), however,
I feel that I'm going beyond where I feel comfortable (I'm not really good with
Linux/Unix stuff...)

Additional advice is welcome...

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/




More information about the Python-list mailing list