Follow-up: Win32/Posix file locking

Sheila King sheila at spamcop.net
Wed Aug 29 22:53:57 EDT 2001


On Wed, 29 Aug 2001 20:20:44 -0400 (EDT), Ignacio Vazquez-Abrams
<ignacio at openservices.net> wrote in comp.lang.python in article
<mailman.999131050.12210.python-list at python.org>:

:On Wed, 29 Aug 2001, Sheila King wrote:

:>     def getReadLock(self):
:>         start_time = time()
:>         while time() - start_time < MAXTIME:
:>             try:
:>                 self.f = open(self.filename, 'r')
:>                 self.fd = self.f.fileno()
:>                 fcntl.lockf(self.fd, fcntl.LOCK_SH | fcntl.LOCK_NB)
:>                 return 1
:>             except:
:>                 self.f.close()
:>                 del self.fd
:
:Ouch. I would put a short delay in here so that CPU usage doesn't go through
:the roof by accident.

What sort of time interval would you suggest? 0.1 seconds, perhaps? Or
is that too short?


:> ----------------------(begin winMutexFile.py)----------------------
:>  ...
:> import os
:> from time import time
:>
:> try:
:>     import win32file
:>     from win32con import GENERIC_READ, GENERIC_WRITE,\
:>         FILE_SHARE_READ, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL
:> except ImportError, e:
:>     print e
:>     print "winMutexFile.py requires ActiveState.com Python win32
:> extensions"
:
:Hmm. I'm thinking that you need some sort of a raise here so that it stops.

Yikes! Thanks for catching that. Duh.

:
:Looks good. Just two other things:
:
:1) I'm wondering if assigning None to fd might be faster than deleting it and
:     recreating it.
:2) The error messages use string concatenation when using a format string
:     might be cleaner.
:
:Other than those few things it looks very good.

Thanks for looking it over, and for the suggestions.
One other thing I've decided to change, is to move the file open out of
the loop in getReadLock in the posixMutexFile.py module. Somewhere in
the newsgroup someone posted in an article about a multi-thread
profiler, that file opening is expensive. So, I changed the way I
handled that a bit.

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




More information about the Python-list mailing list