make it record source of lock holding, print if required

this is mostly intended for devs
This commit is contained in:
steffen123 2010-07-10 20:10:04 +02:00
parent ca61189706
commit e9346f6b82
3 changed files with 15 additions and 6 deletions

View File

@ -50,7 +50,7 @@ class GuiBulkImport():
ttime = None ttime = None
# Does the lock acquisition need to be more sophisticated for multiple dirs? # Does the lock acquisition need to be more sophisticated for multiple dirs?
# (see comment above about what to do if pipe already open) # (see comment above about what to do if pipe already open)
if self.settings['global_lock'].acquire(False): # returns false immediately if lock not acquired if self.settings['global_lock'].acquire(wait=False, source="GuiBulkImport"): # returns false immediately if lock not acquired
#try: #try:
print "\nGlobal lock taken ..." print "\nGlobal lock taken ..."
self.progressbar.set_text("Importing...") self.progressbar.set_text("Importing...")

View File

@ -814,12 +814,13 @@ class fpdb:
def not_implemented(self, widget, data=None): def not_implemented(self, widget, data=None):
self.warning_box("Unimplemented menu entry") self.warning_box("Unimplemented menu entry")
def obtain_global_lock(self): def obtain_global_lock(self, source):
ret = self.lock.acquire(False) # will return false if lock is already held ret = self.lock.acquire(source=source) # will return false if lock is already held
if ret: if ret:
print "\nGlobal lock taken ..." print "\nGlobal lock taken by", source
self.lockTakenBy=source
else: else:
print "\nFailed to get global lock." print "\nFailed to get global lock, it is currently held by", source
return ret return ret
# need to release it later: # need to release it later:
# self.lock.release() # self.lock.release()
@ -841,6 +842,7 @@ class fpdb:
def release_global_lock(self): def release_global_lock(self):
self.lock.release() self.lock.release()
self.lockTakenBy=None
print "Global lock released.\n" print "Global lock released.\n"
def tab_auto_import(self, widget, data=None): def tab_auto_import(self, widget, data=None):

View File

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Code from http://ender.snowburst.org:4747/~jjohns/interlocks.py # Code from http://ender.snowburst.org:4747/~jjohns/interlocks.py
# Thanks JJ! # Thanks JJ!
@ -34,19 +35,24 @@ class InterProcessLockBase:
if not name: if not name:
name = sys.argv[0] name = sys.argv[0]
self.name = name self.name = name
self.heldBy = None
def getHashedName(self): def getHashedName(self):
return base64.b64encode(self.name).replace('=','') return base64.b64encode(self.name).replace('=','')
def acquire_impl(self, wait): abstract def acquire_impl(self, wait): abstract
def acquire(self, wait=False, retry_time=1): def acquire(self, wait=False, retry_time=1, source=None):
if source == None:
source="Unknown"
if self._has_lock: # make sure 2nd acquire in same process fails if self._has_lock: # make sure 2nd acquire in same process fails
print "lock already held by:",self.heldBy
return False return False
while not self._has_lock: while not self._has_lock:
try: try:
self.acquire_impl(wait) self.acquire_impl(wait)
self._has_lock = True self._has_lock = True
self.heldBy=source
#print 'i have the lock' #print 'i have the lock'
except SingleInstanceError: except SingleInstanceError:
if not wait: if not wait:
@ -58,6 +64,7 @@ class InterProcessLockBase:
def release(self): def release(self):
self.release_impl() self.release_impl()
self._has_lock = False self._has_lock = False
self.heldBy=None
def locked(self): def locked(self):
if self.acquire(): if self.acquire():