仅在完成写入后才使用 .hdf5 文件

Using .hdf5 files only once they are finished writing

我正在尝试使用 .hdf5 文件,一旦它们完成写入(在我的例子中,试图发出它们)。但问题是我没有办法 1) 测试他们是否写完了 2) 然后发送他们。我一直尝试使用的代码如下:

    while True:
        event = self._q.get()
        while True:
            try:
                file = h5py.File(event.src_path, "r")
                file.close()
                self.new_file.emit(event.src_path, os.path.basename(event.src_path))
                break
            except OSError:
                if retry_count < max_retry_count:
                    retry_count += 1
                    print(f"h5 file <{event.src_path}> is locked, retrying {retry_count}/{max_retry_count}")
                    time.sleep(retry_interval_seconds)
                else:
                    print(f"h5 file <{event.src_path}> reached max retry count, skipping")

            except Exception as err:
                print(f"Got unexpected Error <{type(err).__name__}> while opening <{event.src_path}> ")
                traceback.print_exc()

显然 break 有问题。但是如果没有中断,try 将停留在循环中并一遍又一遍地发出相同的文件。此代码测试它们是否完美地完成了编写,但是发送它们并继续接收新文件的能力不起作用。非常感谢任何见解。

我通过以下代码解决了这个问题:

    while True:
        event = self._q.get()
        max_retry_count = 350  # for test purposes now but want to set an upper bound on verifying a file is finished.
        retry_interval_seconds = .01  # every hundreth it will try the file to see if it finished writing
        retry_count = 0
        if event.event_type == "created" and event.src_path.lower().endswith(".hdf5"):
            while True:
                try:
                    file = h5py.File(event.src_path, "r")
                    file.close()
                except OSError:
                    if retry_count < max_retry_count:
                        retry_count += 1
                        print(f"h5 file <{event.src_path}> is locked, retrying {retry_count}/{max_retry_count}")
                        time.sleep(retry_interval_seconds)
                    else:
                        print(f"h5 file <{event.src_path}> reached max retry count, skipping")
                        break  # <--- looks useful here
                except Exception as err:
                    print(f"Got unexpected Error <{type(err).__name__}> while opening <{event.src_path}> ")
                    traceback.print_exc()
                else:
                    self.new_file.emit(event.src_path, os.path.basename(event.src_path))
                    break