我有一个简单的示例,该示例以SWRM模式写入文件,但没有将其关闭:
import h5py
import numpy as np
f = h5py.File("swmr.h5", 'w', libver='latest')
arr = np.array([1,2,3,4])
dset = f.create_dataset("data", chunks=(2,), maxshape=(None,), data=arr)
f.swmr_mode = True
# Now it is safe for the reader to open the swmr.h5 file
for i in range(5):
new_shape = ((i+1) * len(arr), )
dset.resize( new_shape )
dset[i*len(arr):] = arr
dset.flush()
# Notify the reader process that new data has been written
input("press enter")
阅读器代码如下:
import h5py
print(h5py.version.info)
f = h5py.File("swmr.h5", 'r', libver='latest', swmr=True)
print(f["data"][:])
但是,直到在Python脚本中成功关闭该文件,该文件才在h5view中不可用
该文件在打开以进行写入时是可读的,但只有在构造中使用swmr_mode = True打开以进行读取时才可读取。 (好吧,这个好主意)
Summary of the h5py configuration
---------------------------------
h5py 2.9.0
HDF5 1.10.4
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]
sys.platform win32
sys.maxsize 9223372036854775807
numpy 1.13.3
如果在打开文件时未启用swmr_mode,并且脚本未正确关闭,则会得到以下信息:
OSError: Unable to open file (file is already open for write (may use <h5clear file> to clear file consistency flags))