[英]Python h5py swmr mode: Can't read data while writer has the file open
我尝试了一个简单的 h5py SWMR 模式示例,但出现了意外行为:
以下编写器脚本使用 h5py 库的 Single-Writer-Multiple-Reader 模式写入 h5 文件:
import h5py
import time
print("Starting")
f = h5py.File('/mnt/c/files/temp.h5', 'w', libver='latest')
f.swmr_mode = True
ncols = 6
grp = f.create_group('test')
dset = grp.create_dataset('dat', chunks=(1,ncols), maxshape=(None,ncols), data=[[1]*ncols])
dset.flush()
print("Sleeping")
time.sleep(10)
f.close()
print("Closed")
当编写器脚本运行时,如果我们尝试使用以下命令从 h5 文件中读取:
import h5py
f = h5py.File("c:/files/temp.h5", 'r', libver='latest', swmr=True)
grps = list(f.keys())
print(grps)
if len(grps) > 0:
grp=f[grps[0]]
dsets = list(grp.keys())
print(dsets)
if len(dsets) > 0:
ds = grp[dsets[0]]
print(ds[:])
f.close()
我们在文件 f 中看不到任何键。
但是,一旦写入器完成运行并关闭文件,读取器就可以读取写入文件的数据。 SWMR 模式的全部意义在于能够在编写器写入文件的同时进行读取。 我是否正确实现了代码,或者库中是否存在错误?
我相信您的问题是您在将 swmr_mode 设置为 ture 后调用了 create_dataset。
来自http://docs.h5py.org/en/stable/swmr.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.