【发布时间】:2021-09-13 21:53:07
【问题描述】:
数据将保存到 hdf5 文件中,但一个文件的保存总共需要大约 30 秒。一旦数据完成保存在一个 hdf5 文件中,该文件将立即使用,直到下一个 hdf5 文件完成,该过程将继续如此。有没有一种简单的方法来检查 hd5 文件是否已完成加载,然后才能使用它? hdf5 文件大约 10-20MB 并且都将保存在同一个文件夹中。当然,我或许可以将某个计时器设置为 30 秒以上,但我有兴趣保持时间尽可能短,这意味着我需要准确知道每个 hdf5 文件何时完成数据采集。
我有几个想法:
- 测量文件大小从一个时间点到另一个时间点的差异。如果没有变化,则假定文件已加载完毕。
- 我对 hdf5 文件了解不多,但也许每个 hdf5 文件的末尾都有一些东西,而且只是在末尾。如果是这种情况,我可以继续检查最后一个组件的值是否存在。如果存在,则该文件必须完成。
有什么想法吗?如果有任何帮助,我将不胜感激。
编辑:
我对 on_created 内部 hdf5 部分的想法:
class CustomHandler(FileSystemEventHandler):
def __init__(self, callback: Callable):
self.callback = callback
# Store callback to be called on every on_created event
def on_created(self, event: Union[DirCreatedEvent, FileCreatedEvent]):
#print(f"Event type: {event.event_type}\nAt: {event.src_path}\n")
# check if it's File creation, not Directory creation
if isinstance(event, FileCreatedEvent):
file = pathlib.Path(event.src_path)
#print(f"Processing file {file.name}\n")
# call callback
#self.callback(file)
wait = 3
max_wait = 30
waited = 0
while True:
try:
h5py.File(self.callback(file), 'r')
return self.callback(file)
except FileNotFoundError:
print('Error: HDF5 File not found')
return None
except OSError:
if waited < max_wait:
print(f'Error: HDF5 File locked, sleeping {wait} seconds...')
time.sleep(wait)
waited += wait
else:
print(f'waited too long= {waited} secs')
return None
【问题讨论】:
-
hd5 文件是如何写入的?通过外部程序、您可以控制的程序还是同一个程序?
-
另外,每个文件是由不同的程序/进程编写的吗?还是同一个程序?
-
这是一个写入hdf5文件的内部程序,但是这个程序和我要使用的程序之间应该没有通信。内部程序完成写入文件后,只能使用 hdf5 文件。每个文件都是由同一个程序编写的。