示例#1
0
class FileLock(object):
    """ A file locking mechanism that has context-manager support so 
        you can use it in a with statement. This should be relatively cross
        compatible as it doesn't rely on msvcrt or fcntl for the locking.
    """
 
    def __init__(self, file_name, timeout=10, delay=.05):
        """ Prepare the file locker. Specify the file to lock and optionally
            the maximum timeout and the delay between each attempt to lock.
        """
        self.is_locked = False
        self.lockfile = "%s.lock" % file_name
        self.file_name = file_name
        self.timeout = timeout
        self.delay = delay
        self.jfile = JChartFile()
 
 
    def acquire(self):
        """ Acquire the lock, if possible. If the lock is in use, it check again
            every `wait` seconds. It does this until it either gets the lock or
            exceeds `timeout` number of seconds, in which case it throws 
            an exception.
        """
        start_time = time.time()
        while True:
            try:
                self.fd = os.open(self.lockfile, os.O_CREAT|os.O_EXCL|os.O_RDWR)
                self.jfile.change_perm(self.lockfile)
                break
            except OSError, e:
                if e.errno != errno.EEXIST:
                    #raise 
                    return False
                try:
                    # check file creation/modification time
                    last_modified_time = os.path.getmtime(self.lockfile)
                    if (time.time() - last_modified_time) > 60 * 60 * 2:
                        # if file was created more than an two hours ago lets remove it. possibly it was not deleted
                        os.unlink(self.lockfile)
                        continue
                except OSError:
                    return False
                if (time.time() - start_time) >= self.timeout:
                    #raise FileLockException("Timeout occured.")
                    return False
                time.sleep(self.delay)
        self.is_locked = True
        return True
示例#2
0
 def __init__(self, file_name, timeout=10, delay=.05):
     """ Prepare the file locker. Specify the file to lock and optionally
         the maximum timeout and the delay between each attempt to lock.
     """
     self.is_locked = False
     self.lockfile = "%s.lock" % file_name
     self.file_name = file_name
     self.timeout = timeout
     self.delay = delay
     self.jfile = JChartFile()