Help: using msvcrt for file locking

Ignacio Vazquez-Abrams ignacio at openservices.net
Sat Aug 25 22:16:39 CEST 2001


On Sat, 25 Aug 2001, Sheila King wrote:

> 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/

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.

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

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>








More information about the Python-list mailing list