gpt4 book ai didi

Python h5py swmr模式: Can't read data while writer has the file open

转载 作者:行者123 更新时间:2023-12-01 09:05:29 29 4
gpt4 key购买 nike

我尝试了 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

  • 处于 SWMR 模式时无法创建新组和数据集。

关于Python h5py swmr模式: Can't read data while writer has the file open,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52093522/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com