コード例 #1
0
ファイル: lock.py プロジェクト: JackonYang/calibre
def singleinstance(name):
    """
    Return True if no other instance of the application identified by name is running,
    False otherwise.
    @param name: The name to lock.
    @type name: string
    """
    if iswindows:
        mutexname = "mutexforsingleinstanceof" + __appname__ + name
        mutex = win32event.CreateMutex(None, False, mutexname)
        err = win32api.GetLastError()
        if err == winerror.ERROR_ALREADY_EXISTS:
            # Close this handle other wise this handle will prevent the mutex
            # from being deleted when the process that created it exits.
            win32api.CloseHandle(mutex)
        elif mutex and err != winerror.ERROR_INVALID_HANDLE:
            atexit.register(win32api.CloseHandle, mutex)
        return not err == winerror.ERROR_ALREADY_EXISTS
    else:
        path = os.path.expanduser("~/." + __appname__ + "_" + name + ".lock")
        try:
            f = open(path, "w")
            fcntl.lockf(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
            atexit.register(_clean_lock_file, f)
            return True
        except IOError:
            return False

    return False
コード例 #2
0
def singleinstance(name):
    '''
    Return True if no other instance of the application identified by name is running,
    False otherwise.
    @param name: The name to lock.
    @type name: string
    '''
    if iswindows:
        mutexname = 'mutexforsingleinstanceof'+__appname__+name
        mutex =  win32event.CreateMutex(None, False, mutexname)
        err = win32api.GetLastError()
        if err == winerror.ERROR_ALREADY_EXISTS:
            # Close this handle other wise this handle will prevent the mutex
            # from being deleted when the process that created it exits.
            win32api.CloseHandle(mutex)
        elif mutex and err != winerror.ERROR_INVALID_HANDLE:
            atexit.register(win32api.CloseHandle, mutex)
        return not err == winerror.ERROR_ALREADY_EXISTS
    else:
        path = os.path.expanduser('~/.'+__appname__+'_'+name+'.lock')
        try:
            f = open(path, 'w')
            fcntl.lockf(f.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB)
            atexit.register(_clean_lock_file, f)
            return True
        except IOError:
            return False

    return False
コード例 #3
0
ファイル: lock.py プロジェクト: JackonYang/calibre
 def __enter__(self):
     self.file = WindowsExclFile(self.path, self.timeout) if iswindows else unix_open(self.path)
     self.file.seek(0)
     timeout = self.timeout
     if not iswindows:
         while self.timeout < 0 or timeout >= 0:
             try:
                 fcntl.lockf(self.file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
                 break
             except IOError:
                 time.sleep(1)
                 timeout -= 1
         if timeout < 0 and self.timeout >= 0:
             self.file.close()
             raise LockError("Failed to lock")
     return self.file
コード例 #4
0
 def __enter__(self):
     self.file = WindowsExclFile(self.path, self.timeout) if iswindows else unix_open(self.path)
     self.file.seek(0)
     timeout = self.timeout
     if not iswindows:
         while self.timeout < 0 or timeout >= 0:
             try:
                 fcntl.lockf(self.file.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB)
                 break
             except IOError:
                 time.sleep(1)
                 timeout -= 1
         if timeout < 0 and self.timeout >= 0:
             self.file.close()
             raise LockError('Failed to lock')
     return self.file