Skip to content

Instantly share code, notes, and snippets.

@cpascual
Created May 23, 2016 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cpascual/026f115091e7fd45057f10b279110ce7 to your computer and use it in GitHub Desktop.
Save cpascual/026f115091e7fd45057f10b279110ce7 to your computer and use it in GitHub Desktop.
import h5py
import numpy
import time
import sys
from multiprocessing import Process
FNAME = 'foo.h5'
class Reader(Process):
"""Just a process that opens the file for reading and keeps it open
Note: I use a separate process to avoid a "File already Open" exception
"""
def run(self):
print "READER: Open file for read"
f = h5py.File(FNAME, 'r', libver='latest')
time.sleep(100)
f.close()
def open_for_read():
""" open the file for read and keep it open"""
reader = Reader()
reader.start()
time.sleep(1) # just give time to the reader to open before continuing
return reader
def initialize_file():
"""create a file containing a dataset"""
f = h5py.File(FNAME,'w', libver='latest')
f.create_dataset('data', data=numpy.arange(5))
f.close()
def open_for_append():
"""open the file for appending"""
f = h5py.File(FNAME, 'a', libver='latest')
f.close()
if __name__ == '__main__':
print "hdf5_version=", h5py.version.hdf5_version
initialize_file()
reader = open_for_read()
try:
# open for append while it is open for read by another process
# fails with hdf5_version=1.10.0 (but not with 1.8.16)
open_for_append()
finally:
reader.terminate()
print "OK. Finish"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment