Help: using msvcrt for file locking

Sheila King sheila at spamcop.net
Sat Aug 25 15:30:16 EDT 2001


I'm trying to do some file locking on Windows. I'm running Win98, but I
hope that the methods in the msvcrt module will work OK on any win32?

I thought I understood what I was doing. I had tested a bit of file
locking stuff.

I opened up two separate MSDOS console windows, and tried acquiring
locks and releasing locks, etc...

In one window:

E:\Apache\cgi-bin\pylinks>python
Python 2.1.1 (#20, Jul 20 2001, 01:19:29) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> import msvcrt, os, sys
>>> f = open('lock.txt')
>>> fd = f.fileno()
>>> size = os.path.getsize('lock.txt')
>>> from time import localtime
>>> msvcrt.locking(fd, msvcrt.LK_RLCK, size)
>>> msvcrt.locking(fd, msvcrt.LK_UNLCK, size)
>>> localtime()
(2001, 8, 25, 12, 14, 19, 5, 237, 1)
>>> msvcrt.locking(fd, msvcrt.LK_RLCK, size)
>>>


In the other window:
E:\Apache\cgi-bin\pylinks>python
Python 2.1.1 (#20, Jul 20 2001, 01:19:29) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> import msvcrt, os, sys
>>> f = open('lock.txt')
>>> fd = f.fileno()
>>> size = os.path.getsize('lock.txt')
>>> from time import localtime
>>> msvcrt.locking(fd, msvcrt.LK_RLCK, size)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IOError: [Errno 36] Resource deadlock avoided
>>> localtime()
(2001, 8, 25, 12, 13, 24, 5, 237, 1)
>>> msvcrt.locking(fd, msvcrt.LK_RLCK, size)
>>> msvcrt.locking(fd, msvcrt.LK_UNLCK, size)
>>>


When one process had the lock on the file first, the other process would
either time out and given an IOError, or else wait until the first
process unlocked the file, and then it appeared to acquire the lock.

Even very simply, in a single window, I can do this and at least get no
error messages:
>>> f = open(r'e:\apache\cgi-bin\pylinks\lock.txt')
>>> fd = f.fileno()
>>> size = os.path.getsize(r'e:\apache\cgi-bin\pylinks\lock.txt')
>>> msvcrt.locking(fd, msvcrt.LK_RLCK, size)
>>> msvcrt.locking(fd, msvcrt.LK_UNLCK, size)
>>> 

I realize that this doesn't prove I had a lock on anything, but I've
tested that out separately, and I believe I am acquiring a lock and
releasing it without error.

NOW, why doesn't this class that I've written work without error?

Here is the class:
---------------(begin winLock module)--------------------
''' winLock.py

class lockobject
supports the same function calls as
posixLock.lockobject
export to lockobject.py: a wrapper module
around the win and posix lockobjects
'''
import os, msvcrt

modes = {'write':msvcrt.LK_RLCK, 'read':msvcrt.LK_NBLCK,\
         'unlock':msvcrt.LK_UNLCK}

class lockobject:
    def _lockoperation(self, lockfilename, mode):
        size = os.path.getsize(lockfilename)
        f = open(lockfilename, 'r')
        f.seek(0)
        msvcrt.locking(f.fileno(), modes[mode], size)
        
    def getReadLock(self, lockfilename):
        self._lockoperation(lockfilename, 'read')

    def getWriteLock(self, lockfilename):
        self._lockoperation(lockfilename, 'write')

    def unlock(self, lockfilename):
        self._lockoperation(lockfilename, 'unlock')

--------------(end of winLock module)-------------------------

Here is an interactive session with the module:

>>> import winLock
>>> from winLock import lockobject
>>> one = lockobject()
>>> one.getWriteLock(r'e:\apache\cgi-bin\pylinks\lock.txt')
>>> one.unlock(r'e:\apache\cgi-bin\pylinks\lock.txt')
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "e:\apache\cgi-bin\pylinks\winLock.py", line 28, in unlock
    self._lockoperation(lockfilename, 'unlock')
  File "e:\apache\cgi-bin\pylinks\winLock.py", line 19, in
_lockoperation
    msvcrt.locking(f.fileno(), modes[mode], size)
IOError: [Errno 13] Permission denied
>>> 

Now, why am I getting this Permission denied error? I'm only doing the
same things I did before without the class? (OK, I did put a seek(0)
command in there. It didn't used to be there and I was getting the same
error, so I though maybe I should set the file pointer to the beginning
of the file each time? Anyhow, doesn't work either way.)

If someone can give me some tips here, I'd really appreciate it.

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




More information about the Python-list mailing list