简体   繁体   中英

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:

The following writer script writes to an h5 file using the Single-Writer-Multiple-Reader mode of h5py library:

    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:

    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.

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. 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.

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

  • new groups and datasets cannot be created when in SWMR mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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