简体   繁体   English

Python h5py swmr模式:写入器打开文件时无法读取数据

[英]Python h5py swmr mode: Can't read data while writer has the file open

I tried a simple example of h5py SWMR mode, and got unexpected behavior:我尝试了一个简单的 h5py SWMR 模式示例,但出现了意外行为:

The following writer script writes to an h5 file using the Single-Writer-Multiple-Reader mode of h5py library:以下编写器脚本使用 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")

While the writer script is running, if we try reading from the h5 file using:当编写器脚本运行时,如果我们尝试使用以下命令从 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()

We don't see any keys in the file f.我们在文件 f 中看不到任何键。

However, once the writer finishes running and closes the file, then the reader is able to read the data that was written to the file.但是,一旦写入器完成运行并关闭文件,读取器就可以读取写入文件的数据。 The whole point of SWMR mode is to be able to simultaneously read while the writer is writing to a file. SWMR 模式的全部意义在于能够在编写器写入文件的同时进行读取。 Am I implementing the code correctly, or is there a bug in the library?我是否正确实现了代码,或者库中是否存在错误?

I believe your problem is that you are calling create_dataset after setting swmr_mode to ture.我相信您的问题是您在将 swmr_mode 设置为 ture 后调用了 create_dataset。

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

  • new groups and datasets cannot be created when in SWMR mode.在 SWMR 模式下无法创建新组和数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM