简体   繁体   English

由于文件关闭不当导致的 h5py OSerror

[英]h5py OSerror due to improper file closing

I have some code that runs a simulation for a certain number of time points, and repeats the process a certain number of times.我有一些代码可以在一定数量的时间点运行模拟,并重复该过程一定次数。 To save my data I use a h5py dataset that i create in lines 10-11.为了保存我的数据,我使用了我在第 10-11 行中创建的 h5py 数据集。 The I run the calculation a certain number of times inside the for loop, and write to the h5py file.我在 for 循环内运行计算一定次数,并写入 h5py 文件。 This seems to work fine on my computer for time_points = np.arange(10**1) and n_simulations = 10**1 .对于time_points = np.arange(10**1)n_simulations = 10**1这似乎在我的计算机上time_points = np.arange(10**1) In line 23, gillespie_ssa is some other function that produces an array as an output and writes it to the appropriate location in the h5py file.在第 23 行中,gillespie_ssa 是其他一些函数,它生成一个数组作为输出并将其写入 h5py 文件中的适当位置。

def prod_out(k):
    args = np.array(k)
    time_points = np.arange(10**1)
    n_simulations = 10**1 # no. of simulsations
    nch = k[2] 
    fval = int(k[0]/k[1])

    with h5py.File("./pops%d.hdf5" %fval, "a") as locals()['hdf5_store_{}'.format(fval)]:
        locals()['pops_{}'.format(fval)] = locals()['hdf5_store_{}'.format(fval)].require_dataset('pops_{}'.format(fval),(n_simulations, len(time_points),2), dtype='int8' ,compression="gzip", chunks=True)
    # Run the calculations 

    #locals()['hdf5_store_{}'.format(fval)]= h5py.File("./pops%d.hdf5" %fval, "a") 
    #locals()['pops_{}'.format(fval)] = locals()['hdf5_store_{}'.format(fval)].require_dataset('pops_{}'.format(fval),(n_simulations, len(time_points),2), dtype='int8' ,compression="gzip", chunks=True)
    # Run the calculations 
    for i in range(n_simulations):
        x=np.random.randint(0,k[2]) #start each simulation from random initial condition
        population_0 = np.array([x, k[2]-x])

        with h5py.File("./pops%d.hdf5" %fval, "a") as locals()['hdf5_store_{}'.format(fval)]:
            locals()['pops_{}'.format(fval)] = locals()['hdf5_store_{}'.format(fval)].require_dataset('pops_{}'.format(fval),(n_simulations, len(time_points),2), dtype='int8' ,compression="gzip", chunks=True)
            locals()['pops_{}'.format(fval)][i,:,:]=(gillespie_ssa(propensity, update,
                                    population_0, time_points, fval ,args=args))

    return locals()['hdf5_store_{}'.format(fval)].close()

I was then running it on my university's cluster time_points = np.arange(10**12) and n_simulations = 10**3 , however, my job was timed out, and since the program was interrupted, the files i produced, when I tried to read them give the following error:然后我在我大学的集群time_points = np.arange(10**12)n_simulations = 10**3上运行它,但是,我的工作超时了,并且由于程序被中断,我生成的文件,当我试图阅读它们给出以下错误:

OSError: Unable to open file (truncated file: eof = 96, sblock->base_addr = 0, stored_eof = 2048)

I believe this was due to improper closing of hdf5 file?我相信这是由于 hdf5 文件关闭不当造成的? I thought commenting out lines 14 and 15, and using with statement would solve this problem, however, i still get the same error in my test cases when I interrupt the job.我认为注释掉第 14 行和第 15 行,并使用with语句可以解决这个问题,但是,当我中断工作时,我的测试用例中仍然出现相同的错误。 (It yields output if I allow it to finish properly). (如果我允许它正确完成,它会产生输出)。 Does the with statement require explicit .flush() of .close() ; with语句是否需要.close()显式.flush() .close() is this what I am missing?这是我所缺少的吗?

What is a safe way to read and write using h5py file, and to ensure that there is still usable data in the file even if there is an interruption?什么是使用h5py文件读写的安全方法,并确保即使出现中断文件中仍有可用数据?

You may split your job into the smaller portions, saving every part into a different file, finally, merging the files into one large one in the end.您可以将您的工作分成更小的部分,将每个部分保存到不同的文件中,最后,将文件合并为一个大文件。

When running your script you have to check which parts are already finished, and skip doing them again, then eventually you'll come to the completion.运行脚本时,您必须检查哪些部分已经完成,并跳过再次执行它们,然后最终您会完成。

And answering your question: No, hdf5 files are not safely saved when the program termination happens, you have to close them properly yourself.并回答您的问题:不,程序终止时无法安全保存 hdf5 文件,您必须自己正确关闭它们。

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

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