Help: using msvcrt for file locking

Tim Peters tim.one at home.com
Sun Aug 26 04:22:10 EDT 2001


[Sheila King]
> Thanks. I don't have a copy of MSVC 6, so it is hard for me to find the
> information on this.

It's almost impossible, alas.  Here are the MS _locking docs (you'll
probably have to paste this URL back together by hand):

<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/h
tml/_crt__locking.asp>

> 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*.

I'm afraid an hour is nothing, Sheila -- there are gigabytes of info
available there, and it can take months just to become comfortable finding
your way around it.

> 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.)

Sure!  You need to understand, though, that Python is coded in C, and C is
of no help here:  C defines *nothing* about file locking.  That means
there's nothing Python can build on cross-platform either.  Even if the only
thing you cared about were Linux systems, you could spend many weeks
learning all the subtleties involved with file-locking on it alone.  It's a
mess.

You're trying to solve a very difficult problem.  I don't know whether the
recipe you found is reliable, but years of experience tell me that anything
that short won't work as intended in all cases (for example, I see the
recipe builds on flock on Unixish boxes, and that may be useless if a
network file system gets involved).

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

You'll find that the MS page I linked to above doesn't say much more than
that.  And we're in an impossible bind here:  the MS docs are copyrighted,
so we can't just reproduce them in the Python docs.  In addition, we don't
get to see MS's source code, so the only way we have to fill in the gaps is
the same way *you* have:  try things at random, see what happens, and try to
*guess* whether that's reliable.  This is impossible, so we shuffle the
MS-specific C functions into their own module, give it an unlikely name, and
say "here it is, if you want it -- but you're on your own".  And you are,
and there's no realistic alternative.

> ...
> (Is everyone who tries to use Python under Windows expected
> to have access to the MS VC 6 documentation?

Of course not.  But "msvcrt" means MicroSoft Visual C++ RunTime, and if you
use *that* obscure module, then, yes, you had better know what you're doing.
msvcrt is a module for people comfortable with MSVC; it's not suitable for
others.  Indeed, there's *no* function in that module I'd recommend for
non-expert use.

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

Python doesn't handle file locking.  Perl pretends to <wink>.  If I were you
I'd try another approach (for example, if your program has control over
access to the files of interest, you can invent your own locking
*abstraction* in software and leave painful OS-specific gimmicks out of it
entirely).





More information about the Python-list mailing list