python - 使用 h5py 高级接口(interface)时如何设置缓存设置?

标签 python h5py

我正在尝试增加我的 HDF5 文件的缓存大小,但它似乎不起作用。 这是我的:

import h5py

with h5py.File("test.h5", 'w') as fid:
        # cache settings of file
        cacheSettings = list(fid.id.get_access_plist().get_cache())
        print cacheSettings
        # increase cache
        cacheSettings[2] = int(5 * cacheSettings[2])
        print cacheSettings
        # read cache settings from file
        fid.id.get_access_plist().set_cache(*cacheSettings)
        print fid.id.get_access_plist().get_cache()

这是输出:

[0, 521, 1048576, 0.75]
[0, 521, 5242880, 0.75]
(0, 521, 1048576, 0.75)

知道为什么读取有效,但设置无效吗?
关闭并重新打开文件似乎也无济于事。

最佳答案

如果您使用的是 h5py 2.9.0 或更高版本,请参阅 Mike's answer .


根据 the docs , get_access_plist() 返回文件访问属性列表的副本。因此,修改副本不会影响原件也就不足为奇了。

高级界面似乎没有提供更改缓存设置的方法。

这是使用低级接口(interface)的方法。

propfaid = h5py.h5p.create(h5py.h5p.FILE_ACCESS)
settings = list(propfaid.get_cache())
print(settings)
# [0, 521, 1048576, 0.75]

settings[2] *= 5
propfaid.set_cache(*settings)
settings = propfaid.get_cache()
print(settings)
# (0, 521, 5242880, 0.75)

上面创建了一个 PropFAID .然后我们可以打开文件并获得 FileID这样:

import contextlib
with contextlib.closing(h5py.h5f.open(
                        filename, flags=h5py.h5f.ACC_RDWR, fapl=propfaid)) as fid:
    # <h5py.h5f.FileID object at 0x9abc694>
    settings = list(fid.get_access_plist().get_cache())
    print(settings)
    # [0, 521, 5242880, 0.75]

我们可以使用 fid 通过将 fid 传递给 h5py.File 来使用高级接口(interface)打开文件:

    f = h5py.File(fid)
    print(f.id.get_access_plist().get_cache())
    # (0, 521, 5242880, 0.75)

因此,您仍然可以使用高级接口(interface),但需要一些时间 摆弄到那里。另一方面,如果你将它提炼成最基本的东西,也许它还不错:

import h5py
import contextlib

filename = '/tmp/foo.hdf5'
propfaid = h5py.h5p.create(h5py.h5p.FILE_ACCESS)
settings = list(propfaid.get_cache())
settings[2] *= 5
propfaid.set_cache(*settings)
with contextlib.closing(h5py.h5f.open(filename, fapl=propfaid)) as fid:
    f = h5py.File(fid)

关于python - 使用 h5py 高级接口(interface)时如何设置缓存设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14653259/

相关文章:

python - 在 Python 中加载 csv 并保存 HDF5

python - 如何在 Django Python 中从子类访问父类字段

python - 显式相对导入的正确样板是什么?

python - 获取 SPY 股价

numpy - 如何连接两个 hdf5 格式的 numpy 数组?

python - 使用 h5py 将外部原始文件链接到 hdf5 文件

python - 在 Django ListView 中更新 get_queryset 后如何更改排序?

python - 回合结束后数值被覆盖,打印时可见

python - 您可以使用组对象提取 hdf5 的文件对象吗?