parameters: (self, timeout, delay)
docstring: Open the file and lock it.
  
      Tries to create a .lock file next to the file we're trying to open.
  
      Args:
        timeout: float, How long to try to lock for.
        delay: float, How long to wait between retries.
  
      Raises:
        AlreadyLockedException: if the lock is already acquired.
        IOError: if the open fails.
docstring_summary: Open the file and lock it.
docstring_tokens: Open the file and lock it .
function: def open_and_lock(self, timeout, delay):
      """Open the file and lock it.
  
      Tries to create a .lock file next to the file we're trying to open.
  
      Args:
        timeout: float, How long to try to lock for.
        delay: float, How long to wait between retries.
  
      Raises:
        AlreadyLockedException: if the lock is already acquired.
        IOError: if the open fails.
      """
      if self._locked:
        raise AlreadyLockedException('File %s is already locked' %
                                     self._filename)
      self._locked = False
  
      try:
        self._fh = open(self._filename, self._mode)
      except IOError, e:
        # If we can't access with _mode, try _fallback_mode and don't lock.
        if e.errno == errno.EACCES:
          self._fh = open(self._filename, self._fallback_mode)
          return
  
      lock_filename = self._posix_lockfile(self._filename)
      start_time = time.time()
      while True:
        try:
          self._lock_fd = os.open(lock_filename,
                                  os.O_CREAT|os.O_EXCL|os.O_RDWR)
          self._locked = True
          break
  
        except OSError, e:
          if e.errno != errno.EEXIST:
            raise
          if (time.time() - start_time) >= timeout:
            logger.warn('Could not acquire lock %s in %s seconds' % (
                lock_filename, timeout))
            # Close the file and open in fallback_mode.
            if self._fh:
              self._fh.close()
            self._fh = open(self._filename, self._fallback_mode)
            return
          time.sleep(delay)
function_tokens: def open_and_lock ( self , timeout , delay ) : if self . _locked : raise AlreadyLockedException ( 'File %s is already locked' % self . _filename ) self . _locked = False try : self . _fh = open ( self . _filename , self . _mode ) except IOError , e : # If we can't access with _mode, try _fallback_mode and don't lock. if e . errno == errno . EACCES : self . _fh = open ( self . _filename , self . _fallback_mode ) return lock_filename = self . _posix_lockfile ( self . _filename ) start_time = time . time ( ) while True : try : self . _lock_fd = os . open ( lock_filename , os . O_CREAT | os . O_EXCL | os . O_RDWR ) self . _locked = True break except OSError , e : if e . errno != errno . EEXIST : raise if ( time . time ( ) - start_time ) >= timeout : logger . warn ( 'Could not acquire lock %s in %s seconds' % ( lock_filename , timeout ) ) # Close the file and open in fallback_mode. if self . _fh : self . _fh . close ( ) self . _fh = open ( self . _filename , self . _fallback_mode ) return time . sleep ( delay )