示例#1
0
    def acquire(self, delay=0.01):
        if delay < 0:
            raise ValueError("Delay must be greater than or equal to zero")

        basedir = os.path.dirname(self.fname)
        if not os.path.exists(basedir):
            fileutils.ensure_tree(basedir)
            LOG.info(_LI('Created lock path: %s'), basedir)

        # Open in append mode so we don't overwrite any potential contents of
        # the target file.  This eliminates the possibility of an attacker
        # creating a symlink to an important file in our lock_path.
        self.lockfile = open(self.fname, 'a')
        start_time = time.time()

        # Using non-blocking locks (with retries) since green threads are not
        # patched to deal with blocking locking calls. Also upon reading the
        # MSDN docs for locking(), it seems to have a 'laughable' 10
        # attempts "blocking" mechanism.
        do_acquire = _lock_retry(delay=delay,
                                 filename=self.fname)(self.trylock)
        do_acquire()
        self.acquire_time = time.time()
        LOG.debug('Acquired file lock "%s" after waiting %0.3fs', self.fname,
                  (self.acquire_time - start_time))

        return True
示例#2
0
    def acquire(self, delay=0.01):
        if delay < 0:
            raise ValueError("Delay must be greater than or equal to zero")

        basedir = os.path.dirname(self.fname)
        if not os.path.exists(basedir):
            fileutils.ensure_tree(basedir)
            LOG.info(_LI('Created lock path: %s'), basedir)

        # Open in append mode so we don't overwrite any potential contents of
        # the target file.  This eliminates the possibility of an attacker
        # creating a symlink to an important file in our lock_path.
        self.lockfile = open(self.fname, 'a')
        start_time = time.time()

        # Using non-blocking locks (with retries) since green threads are not
        # patched to deal with blocking locking calls. Also upon reading the
        # MSDN docs for locking(), it seems to have a 'laughable' 10
        # attempts "blocking" mechanism.
        do_acquire = _lock_retry(delay=delay,
                                 filename=self.fname)(self.trylock)
        do_acquire()
        self.acquire_time = time.time()
        LOG.debug('Acquired file lock "%s" after waiting %0.3fs',
                  self.fname, (self.acquire_time - start_time))

        return True
def remove_external_lock_file(name, lock_file_prefix=None, lock_path=None,
                              semaphores=None):
    """Remove an external lock file when it's not used anymore
    This will be helpful when we have a lot of lock files
    """
    with internal_lock(name, semaphores=semaphores):
        lock_file_path = _get_lock_path(name, lock_file_prefix, lock_path)
        try:
            os.remove(lock_file_path)
        except OSError:
            LOG.info(_LI('Failed to remove file %(file)s'),
                     {'file': lock_file_path})
示例#4
0
def remove_external_lock_file(name, lock_file_prefix=None, lock_path=None,
                              semaphores=None):
    """Remove an external lock file when it's not used anymore
    This will be helpful when we have a lot of lock files
    """
    with internal_lock(name, semaphores=semaphores):
        lock_file_path = _get_lock_path(name, lock_file_prefix, lock_path)
        try:
            os.remove(lock_file_path)
        except OSError:
            LOG.info(_LI('Failed to remove file %(file)s'),
                     {'file': lock_file_path})