这是给你的 HDF5和多处理专家...首先,我知道 python h5py 和多处理模块不一定彼此喜欢,但是我遇到了一个我无法弄清楚的错误。我正在处理的脚本创建一个临时的内存中hdf5 文件,将来自(pickle)输入文件的数据存储在内存文件中,然后一个多处理池对来自 temp 的数据执行(只读)操作HDF5 文件。
我已经能够隔离导致错误的代码,所以这里有一个简化的代码片段。当我在生成器函数中创建内存中的hdf5 文件,然后使用该生成器为多处理池生成参数时,我得到了一系列 HDF5 错误。这里有一些代码很简单,我可以让它重新创建错误:
import h5py
from multiprocessing import Pool
from itertools import imap
useMP = True
def doNothing(arg):
print "Do nothing with %s"%arg
def myGenerator():
print "Create hdf5 in-memory file..."
hdfFile = h5py.File('test.hdf',driver='core',backing_store=False)
print "Finished creating hdf5 in-memory file."
yield 3.14159
'''
# uncommenting this section will result in yet another HDF5 error.
print "Closing hdf5 in-memory file..."
hdfFile.close()
print "Finished closing hdf5 in-memory file."
'''
if useMP:
pool = Pool(1)
mapFunc = pool.imap
else:
mapFunc = imap
data = [d for d in mapFunc(doNothing,myGenerator())]
当我使用 itertools.imap(设置“useMP=False”)时,我得到以下输出,如预期的那样:
Create hdf5 in-memory file...
Finished creating hdf5 in-memory file.
Do nothing with 0
但是当我使用 Pool.imap 时,即使池仅使用单个工作线程创建,我也会得到以下输出:
Create hdf5 in-memory file...
HDF5-DIAG: Error detected in HDF5 (1.8.9) thread 139951680009984:
#000: ../../../src/H5F.c line 1538 in H5Fopen(): unable to open file
major: File accessability
minor: Unable to open file
#001: ../../../src/H5F.c line 1227 in H5F_open(): unable to open file: time = Wed Feb 27 09:32:32 2013
, name = 'test.hdf', tent_flags = 1
major: File accessability
minor: Unable to open file
#002: ../../../src/H5FD.c line 1101 in H5FD_open(): open failed
major: Virtual File Layer
minor: Unable to initialize object
#003: ../../../src/H5FDcore.c line 464 in H5FD_core_open(): unable to open file
major: File accessability
minor: Unable to open file
Finished creating hdf5 in-memory file.
Do nothing with 0
奇怪的是,这个错误不会使程序崩溃。我正在编写的导致此错误的脚本实际上按我的预期工作 - 但它为它创建的每个内存文件提供了上述错误。使用 itertools.imap 时没有错误,读取现有 HDF5 文件时没有错误,只有多处理和内存中 HDF5 文件的组合。
h5py 版本 2.1.1
hdf5 版本 1.8.9
Python 版本 2.7.3