Help: using msvcrt for file locking

Sheila King sheila at spamcop.net
Wed Aug 29 19:07:55 EDT 2001


David,

Thanks for a very clear and thorough explanation on the idea of atomic
operations in the file system. (I finally took the time today to read
through this thoroughly.) I have some additional questions below.

On 28 Aug 2001 03:13:11 -0400, David Bolen <db3l at fitlinxx.com> wrote in
comp.lang.python in article <uheuslks8.fsf at ctwd0143.fitlinxx.com>:

:Atomicity or ensuring that an operation is "atomic" refers to the fact
:that the operation in question can be considered a single
:uninterruptible unit of activity, at some level of granularity. 

...<excellent explanations snipped>...

:In many (most? I'm not sure) filesystems, rename is an atomic (through
:other internal locks or procedures) operation to help ensure the
:integrity of the filesystem, at least when renaming is an integral
:part of the filesystem interface.  There are other times when it
:doesn't hold true though, many of which occur when using networked
:filesystems.  Another good odds atomic operation is making a directory
:- that's normally my preference if I have to have a cross-platform
:sentinel file (it should even work with NFS).

So, you're saying that the way you usually handle exclusive access to
objects, is by creating/removing directories? 

:  The big problem with
:any non-active-filehandle approach (like a file or directory) is that
:cleanup is critical if you don't want to block other tasks.  So if
:there's any chance the process managing the lock might die, you need
:some way to remove the lock if that happens.

Yes, I discovered that while testing, today. Ignacio had suggested that
I needed a __del__ function for my classes, and I found out today, that
on Linux, if an object had the lock and was deleted but that process
still kept running, I could not recover the lock. I've written a __del__
function, now, that seems to work. It unlocks the file and does cleanup.

:> Actually, the win32file.CreateFile does have an option for exclusive
:> access. I don't believe there is a specified timeout, but it does have
:> the exclusive access part.
:
:It doesn't have the timeout, thus as my suggested code in another post
:does, you have to implement a retry mechanism yourself.  This leaves a
:small risk of starvation (depending on load and retry mechanism) but
:in most cases it's not a major problem.

I will post my code in another thread, I think. (This one is sooo long
already.) I think I have something that works fairly well, now.

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





More information about the Python-list mailing list