Help: using msvcrt for file locking

Sheila King sheila at spamcop.net
Sun Aug 26 02:54:16 EDT 2001


On Sat, 25 Aug 2001 23:25:42 -0400, "Tim Peters" <tim.one at home.com>
wrote in comp.lang.python in article
<mailman.998796398.21253.python-list at python.org>:

:[Sheila King]
:> 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?
:
:msvcrt directly exposes some library functions supplied by, and unique to,
:Microsoft.  So, like it or not (I don't like it myself <wink>), Microsoft is
:the only entity who can answer questions about them with authority.  FWIW,
:the docs MS supplied with MSVC 6 say their _locking function works under
:"Win 95, Win NT", which *usually* means "everything after Win 3.1".

Thanks. I don't have a copy of MSVC 6, so it is hard for me to find the
information on this. This past week I spent a good hour or so at the
microsoft.com's developers site, searching for more definitive info. I
really didn't find anything that helped *me*, but that could be just
because it's *me*.

:Also as usual, the functions MS provides as part of their C library are a
:feeble subset of what you can do by using the Win32 API directly, in this
:case by using the Win32 LockFile and LockFileEx functions.  I don't know
:whether win32all exposes those specific functions; core Python does not.

I've been pouring over the Active State site, as well. The win32
extensions do, apparently, provide access to the LockFile and LockFileEx
functions.
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203

It's just that I was hoping to accomplish this without requiring the use
of the win32 extensions. (Maybe it's silly of me, but I tend to want to
avoid requiring others to install stuff to use the code.)

...<snipped>...
:> 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)
:
:My guess is that's why:  in your interactive sessions, you kept the file
:open.  In the _lockoperation method above, the file will be closed
:implicitly under CPython the instant _lockoperation returns (because the
:refcount on f falls to 0 then, and an open file object is closed by the file
:object's destructor).  The MS _locking docs say:
:
:    Regions should be locked only briefly and should be unlocked before
:    closing a file or exiting the program.
:
:So if you leave locks sitting around when a file is closed, the behavior is
:undefined.  I happen to get the same error from this program:
:
:import os, msvcrt
:
:NAME = 'empty.txt'
:
:size = os.path.getsize(NAME)
:f = open(NAME, 'r')
:msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, size)
:
:C:\Code\python\PCbuild>python mlock.py
:Traceback (most recent call last):
:  File "mlock.py", line 7, in ?
:    msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, size)
:IOError: [Errno 13] Permission denied
:
:In other words, it's acting the same as if the file had never been locked.
:
:> ...
:> Now, why am I getting this Permission denied error?
:
:The MS docs aren't clear, but what you're trying to do goes beyond even the
:little they do say will work.

OK, first of all, thank you for the discussion above. It gives me a
tremendous insight into what I've been doing wrong and how to handle the
problem.

Second of all, I feel that I am at a tremendous disadvantage, as I don't
have access to the MS documentation that you keep referring to. Here's
what I have access to:
http://www.python.org/doc/current/lib/msvcrt-files.html

Of course, I was only intending to leave the files locked very briefly,
but I didn't realize that having them close after being locked would be
a problem. The Python documentation on this module is even more brief
than the MS documentation that you keep referring to as overly brief and
inadequate. (Is everyone who tries to use Python under Windows expected
to have access to the MS VC 6 documentation? If so, where is it
available?)

:Cross-platform file-locking is extremely difficult, in part because Python
:is coded in C and C defines no facilities for doing it.  I don't know why
:you're trying to lock files, but the evidence suggests you're trying to use
:msvcrt.locking for something it was never intended to do (it was intended
:for programs like databases, where multiple threads and even processes
:cooperatively both read and write to a single file, and sometimes need
:exclusive access for brief periods of time to small regions of the file --
:that's why there's a "size" argument).

I am intending to use this for programs that will access databases.
Specifically, CGI scripts using the Berkeley data base hash. The
Berkeley Sleepy Cat that I have access to doesn't support concurrent
write access. I have to handle the file locking myself.

What you see above is the confusion of someone who is trying to learn
about file locking and handling concurrent access at the same time that
she is attempting to learn how Python handles it AND how to do it cross
platform. No more. No less.

Anyhow, I thank you for your examples. And your quoting from the MS VC 6
documentation. It has helped me immensely.

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





More information about the Python-list mailing list