Follow-up: Win32/Posix file locking
sheila at spamcop.net
Thu Aug 30 04:53:57 CEST 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:
:> self.f = open(self.filename, 'r')
:> self.fd = self.f.fileno()
:> fcntl.lockf(self.fd, fcntl.LOCK_SH | fcntl.LOCK_NB)
:> return 1
:> 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
:> 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
: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.
More information about the Python-list