使用程序读写hdf5文件报错 OSError: [Errno 37] Unable to synchronously create file (unable to lock file, errno = 37, error message = ‘No locks available’)
测试python程序
import h5py
import numpy as np
data_matrix = np.random.uniform(-1, 1, size=(2, 2))
# Write data to HDF5
data_file = h5py.File('file.hdf5', 'w')
data_file.create_dataset('group_name', data=data_matrix)
data_file.close()
filename = 'file.hdf5'
f = h5py.File(filename, 'r')
# List all groups
print("Keys: %s" % f.keys())
a_group_key = list(f.keys())[0]
# Get the data
data = list(f[a_group_key])
print(data)
运行后,报错
Traceback (most recent call last):
File "/opt/data/private/hdf_test/test.py", line 6, in <module>
data_file = h5py.File('file.hdf5', 'w')
File "/opt/conda/lib/python3.10/site-packages/h5py/_hl/files.py", line 562, in __init__
fid = make_fid(name, mode, userblock_size, fapl, fcpl, swmr=swmr)
File "/opt/conda/lib/python3.10/site-packages/h5py/_hl/files.py", line 241, in make_fid
fid = h5f.create(name, h5f.ACC_TRUNC, fapl=fapl, fcpl=fcpl)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py/h5f.pyx", line 122, in h5py.h5f.create
OSError: [Errno 37] Unable to synchronously create file (unable to lock file, errno = 37, error message = 'No locks available')
解决方法:
在终端输入:
export HDF5_USE_FILE_LOCKING=FALSE
再次运行程序返回:
root@interactive90765:/opt/data/private/hdf_test# python test.py
Keys: <KeysViewHDF5 ['group_name']>
[array([ 0.68818786, -0.35534535]), array([-0.99607473, -0.23781375])]
如果通过VSCODE或者PyCharm 客户端远程连接服务器调试程序,需要断开重新连接后,再跑程序。
以上添加只是临时生效,需要永久生效可以将 export HDF5_USE_FILE_LOCKING=FALSE 添加到 .bashrc 后永久生效。