Help: using msvcrt for file locking

Sheila King sheila at spamcop.net
Mon Aug 27 02:14:56 EDT 2001


On Mon, 27 Aug 2001 05:43:02 GMT, bokr at accessone.com (Bengt Richter)
wrote in comp.lang.python in article
<3b89db9b.565789932 at wa.news.verio.net>:

:I'll assume that when you say 'file locking', you mean the file as
:a whole, rather than a region of the file, as the parameters for _locking
:specify (I think an example of yours had parameters for the whole
:file length).

Yes, this is what I was going to do. I wasn't going to lock the file I
want to read to/write from itself, but a 'lockfile' and then, when I
obtain the lock on the lockfile, I would read/write to the datafile
(because underlying implementations of databases vary, and databases are
sometimes comprised of more than one file).

:Have you considered using os.rename() to implement your method #3 above?
:Barring a system crash, renaming should be atomic, and even if there
:is a crash after renaming, you should be able to detect orphan files
:if you put appropriate info in the new name.

Actually, I had thought of that. I had been discussing it with my
husband this afternoon. He didn't think it was a great idea, but he
thought it had possibilities. It makes me feel a bit better, to hear
someone else suggest it.

Actually, I think what happens with Windows98, is that there is only
exclusive lock. No shared locking (which is what one would want for
reading). So, I could change the name of the file when a write process
wants to acquire it. Otherwise, if the un-renamed file is there, it is
OK to read from it. Do you think this type of approach would work for
the reading part only? 

:I'll stick my neck out here with a pretty untested sketch:

Thanks. I will look this code over. I really appreciate the suggestions.

[code snipped]

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




More information about the Python-list mailing list