I am trying to create a generator function to train my machine learning model and yield
some images. Basically it goes like that (the minimum to reproduce the error on my side):
def generator(hdf5_path: str, primary_keys: list):
while True:
with h5py.File(hdf5_path, "r") as f:
for pk in primary_keys:
yield f[pk][0]
gen = generator('/home/coder/images.hdf5', pk=['AAA', 'BBB'])
for i in range(5):
image = next(gen)
print(image)
From this code I am supposed to get 5 images and then the generator
should be shut down closing the h5py
file. But every time the generator is stopped I get this error message
:
Exception ignored in: <generator object generator at 0x7f41b112ab30>
Traceback (most recent call last):
File "/home/coder/workspaces/data_processing.py", line 420, in generator
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 "/home/coder/.local/lib/python3.8/site-packages/h5py/_hl/files.py", line 461, in __exit__
File "/home/coder/.local/lib/python3.8/site-packages/h5py/_hl/files.py", line 432, in close
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 267, in h5py.h5f.get_obj_ids
File "h5py/h5i.pyx", line 37, in h5py.h5i.wrap_identifier
ImportError: sys.meta_path is None, Python is likely shutting down
From what I understand this comes because the generator doesn't close properly but I am not too sure what to do about it ?
I also read that this could be potentially catch using try/error from here on the GeneratorExit
but it didn't work (I can catch it with Exception
but I want to understand more). The other message I read about this error were using a selenium
package which I am not using at all (from here).
Any idea ? Solution/explanation for this behavior ?
Thank you