Форум сайта python.su
class InterProcessLock(object):
def lock(self, delta=0.01):
while True:
try:
self.lockNoWait()
break;
except:
time.sleep(delta)
def __init__(self, name=None):
self.mutex = None
if not name:
name = sys.argv[0]
self.name = base64.b64encode(name).replace('=','')
def lockNoWait(self):
self.mutex = win32event.CreateMutex(None, 0, self.name)
if win32api.GetLastError() == winerror.ERROR_ALREADY_EXISTS:
self.mutex.Close()
self.mutex = None
raise Exception('acquired')
def unlock(self):
self.mutex.Close()
Офлайн
Немного исправленный код:
def lockNoWait(self):
try:
self.mutex = win32event.CreateMutex(None, 0, self.name)
except win32api.error, e:
#print str(e)
if self.mutex is not None:
self.mutex.Close()
self.mutex = None
#else:
#print 'self.mutex == None'
raise Exception('acquired')
Отредактировано (Июль 8, 2011 19:20:02)
Офлайн