Beispiel #1
0
    def _getOpenHdf5Blockfile(self, blockFilePath):
        """
        Return a handle to the open hdf5File at the given path.
        If we haven't opened the file yet, open it first.
        """
        # Try once without locking
        if blockFilePath in list(self._openBlockFiles.keys()):
            return self._openBlockFiles[blockFilePath]

        # Obtain the lock and try again
        with self._lock:
            if blockFilePath not in list(self._openBlockFiles.keys()):
                try:
                    writeLock = FileLock(blockFilePath, timeout=10)
                    if self.mode == "a":
                        acquired = writeLock.acquire(blocking=False)
                        assert acquired, "Couldn't obtain an exclusive lock for writing to file: {}".format(
                            blockFilePath
                        )
                        self._fileLocks[blockFilePath] = writeLock
                    elif self.mode == "r":
                        assert writeLock.available(), "Can't read from a file that is being written to elsewhere."
                    else:
                        assert False, "Unsupported mode"
                    self._openBlockFiles[blockFilePath] = h5py.File(blockFilePath, self.mode)
                except:
                    log_exception(logger, "Couldn't open {}".format(blockFilePath))
                    raise
            return self._openBlockFiles[blockFilePath]
Beispiel #2
0
 def isBlockLocked(self, blockstart):
     """
     Return True if the block is locked for writing.
     Note that both 'available' and 'not available' blocks might be locked.
     """
     datasetPathComponents = self.getDatasetPathComponents(blockstart)
     hdf5FilePath = datasetPathComponents.externalPath
     testLock = FileLock(hdf5FilePath)
     return not testLock.available()