Help: using msvcrt for file locking

Sheila King sheila at spamcop.net
Sat Aug 25 18:25:18 EDT 2001


On Sat, 25 Aug 2001 16:16:39 -0400 (EDT), Ignacio Vazquez-Abrams
<ignacio at openservices.net> wrote in comp.lang.python in article
<mailman.998770657.716.python-list at python.org>:

:I have no experience with msvcrt, but I'm guessing that it might be that
:msvcrt.locking() requires the descriptor to be the same when unlocking as when
:locking.

Thanks. That sounds like a good tip. I will try to work on that. I think
I can figure out a way to handle it.

:Also, where is this posixLock so that I can look at it?

Well, it didn't have any bearing on this question, so I didn't post it.
But it is my own code (not yet tested). Since you ask, here it is:

''' posixLock.py

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

import fcntl

class lockobject:
    def getReadLock(self, lockfilename):
        fcntl.flock(lockfilename.fileno(), fcntl.LOCK_SH)

    def getWriteLock(self, lockfilename):
        fcntl.flock(lockfilename.fileno(), fcntl.LOCK_EX)

    def unlock(self, lockfilename):
        fcntl.flock(lockfilename.fileno(), fcntl.LOCK_UN)



And then there is the wrapper class, lockobject:

''' lockobject.py

a class for supporting file locking across
posix and windows platforms. This class is an abstraction
that wraps the locking details between these two
platforms.

Wrapped methods:
getReadLock(lockfilename)
getWriteLock(lockfilename)
unlock(lockfilename)

A LockObject is an object which will be read from and
written to. However, there may be multiple concurrent
processes trying to access the object. In order to prevent
data corruption, write operations must have exclusive
access. Read operations can occur simultaneously with no ill-effects.
The lockfilename is the name of a file, different from the
LockObject, which the LockObject must obtain an appropriate
type of lock on (a shared lock for reading, an exclusive
lock for writing), before any attempts at reading data from or
writing data to the Lock Object occur.

It is important that when a read or write process has finished,
that the LockObject unlock the lockfile so that other processes
can proceed. (All other processes attempting to read or write
may be blocked, depending on the type of lock.)

Note: On the Windows platforms, when a lock is requested (either
a read or write lock), there will be a maximum of ten attempts
made in one second intervals to obtain a lock on the lockfile. If
all of these attempts fail, the operation will time out and raise
an IOError.
'''

import os

osname = os.name
if osname == "nt":
    from winLock import lockobject
elif osname == "posix":
    from posixLock import lockobject
else:
    raise ImportError, "locking not supported on your platform"

class LockObject(lockobject):
    pass

if __name__ == '__main__':
    print "Testing..."
    lockfile = raw_input("Enter a complete path/filename to test for
locking")
    one = LockObject()
    print "created a LockObject"


I'm just really dissatisfied with the poor file locking support in
Python, unless one is writing code exclusively for posix platforms.
About a week ago, I was composing a rant on that topic to post here. I
did a lot of research on the topic, and a lot of web searches
(google.com, groups.google.com, activestate.com, mail.python.org and so
forth...)

In the end, I decided not to post the rant (I deleted it), since I
didn't think I'd gain anything by it. I decided to just rather try to
solve the problem myself.

Really, though, I do think the problem should be solved by someone in
the Python developers circle and included in the standard library
(something along the lines of anydbm). But, that's just my opinion. I
have a number of reasons why I feel that way, but I won't go into it
right now.

Anyhow, Ignacio, thanks for the idea about the file descriptor. I think
you may have something there. I will try it out later tonight and see if
that helps.

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




More information about the Python-list mailing list