mirror of
https://github.com/paulscherrerinstitute/sf_daq_broker.git
synced 2026-08-10 23:10:29 +02:00
Transition to with statement for writer
In case of exception while writing the file it would not get closed properly previously - file.close() would never have been called.
This commit is contained in:
@@ -46,9 +46,8 @@ def write_from_databuffer(data_api_request, output_file, metadata):
|
||||
|
||||
start_time = time()
|
||||
|
||||
writer = BsreadH5Writer(output_file, metadata)
|
||||
writer.write_data(data)
|
||||
writer.close()
|
||||
with BsreadH5Writer(output_file, metadata) as writer:
|
||||
writer.write_data(data)
|
||||
|
||||
_logger.info("Data writing took %s seconds." % (time() - start_time))
|
||||
|
||||
@@ -106,6 +105,12 @@ class BsreadH5Writer(object):
|
||||
|
||||
self._create_metadata_datasets(metadata)
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
self.close()
|
||||
|
||||
def _create_metadata_datasets(self, metadata):
|
||||
|
||||
_logger.debug("Initializing metadata datasets.")
|
||||
|
||||
Reference in New Issue
Block a user