From 6fcf81e56645505cfda348ddf3fc44dc529bb54f Mon Sep 17 00:00:00 2001 From: fpdb-mme Date: Sun, 1 Nov 2009 09:51:00 +0100 Subject: [PATCH] bugfix: InterProcessLock.locked() was broken cause InterProcessLock.aquire() no longer raises SingleInstanceError - fixed InterProcessLock.locked() - fixed doctests notes: - doctests run ok on linux with InterProcessLockFcntl and InterprocessLockSocket - doctests fail on linux/wine with InterprocessLockWin32 when trying to aquire the lock held by the process created on the fly. just a guess ..maybe the mutex is not released automatically when terminating the process. does native win32 guarantee this? --- pyfpdb/interlocks.py | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/pyfpdb/interlocks.py b/pyfpdb/interlocks.py index b19ff43b..366edd68 100755 --- a/pyfpdb/interlocks.py +++ b/pyfpdb/interlocks.py @@ -60,14 +60,10 @@ class InterProcessLockBase: self._has_lock = False def locked(self): - if self._has_lock: - return True - try: - self.acquire() + if self.acquire(): self.release() return False - except SingleInstanceError: - return True + return True LOCK_FILE_DIRECTORY = '/tmp' @@ -162,38 +158,33 @@ def test_construct(): >>> lock1 = InterProcessLock(name=test_name) >>> lock1.acquire() + True >>> lock2 = InterProcessLock(name=test_name) >>> lock3 = InterProcessLock(name=test_name) # Since lock1 is locked, other attempts to acquire it fail. >>> lock2.acquire() - Traceback (most recent call last): - ... - SingleInstanceError: Could not acquire exclusive lock on /tmp/test.lck + False >>> lock3.acquire() - Traceback (most recent call last): - ... - SingleInstanceError: Could not acquire exclusive lock on /tmp/test.lck + False # Release the lock and let lock2 have it. >>> lock1.release() >>> lock2.acquire() + True >>> lock3.acquire() - Traceback (most recent call last): - ... - SingleInstanceError: Could not acquire exclusive lock on /tmp/test.lck + False # Release it and give it back to lock1 >>> lock2.release() >>> lock1.acquire() + True >>> lock2.acquire() - Traceback (most recent call last): - ... - SingleInstanceError: Could not acquire exclusive lock on /tmp/test.lck + False # Test lock status >>> lock2.locked() @@ -237,15 +228,14 @@ def test_construct(): >>> pid = execute('import interlocks;a=interlocks.InterProcessLock(name=\\''+test_name+ '\\');a.acquire();') >>> lock1.acquire() - Traceback (most recent call last): - ... - SingleInstanceError: Could not acquire exclusive lock on /tmp/test.lck + False >>> os_independent_kill(pid) >>> time.sleep(1) >>> lock1.acquire() + True >>> lock1.release() # Testing wait @@ -253,13 +243,12 @@ def test_construct(): >>> pid = execute('import interlocks;a=interlocks.InterProcessLock(name=\\''+test_name+ '\\');a.acquire();') >>> lock1.acquire() - Traceback (most recent call last): - ... - SingleInstanceError: Could not acquire exclusive lock on /tmp/test.lck + False >>> os_independent_kill(pid) >>> lock1.acquire(True) + True >>> lock1.release() """