【问题标题】:Python h5py swmr mode: Can't read data while writer has the file openPython h5py swmr 模式:写入器打开文件时无法读取数据
【发布时间】:2019-07-19 01:59:12
【问题描述】:

我尝试了一个简单的 h5py SWMR 模式示例,但出现了意外行为:

以下 writer 脚本使用 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 模式的重点是能够在写入器写入文件的同时进行读取。我是否正确实现了代码,还是库中存在错误?

【问题讨论】:

    标签: python h5py


    【解决方案1】:

    我相信您的问题是您在将 swmr_mode 设置为 ture 之后调用了 create_dataset。

    来自http://docs.h5py.org/en/stable/swmr.html

    • 在 SWMR 模式下无法创建新组和数据集。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 2023-01-05
      • 2020-07-27
      • 1970-01-01
      • 2018-08-29
      • 2016-01-23
      相关资源
      最近更新 更多