Skip to content

Commit

Permalink
Open and close h5 database on all uses.
Browse files Browse the repository at this point in the history
  • Loading branch information
brettc committed Oct 3, 2017
1 parent cbd4eb1 commit c3d4546
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions partfinder/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ def run_task(self, model_name, sub):
# Not entirely sure that WE NEED to block here, but it is safer to do
# It shouldn't hold things up toooo long...
self.lock.acquire()
log.debug("Getting lock")
try:
if fabricate:
sub.fabricate_model_result(the_config, model_name)
Expand All @@ -248,6 +249,7 @@ def run_task(self, model_name, sub):
sub.finalise(the_config)
finally:
self.lock.release()
log.debug("Release lock")

def add_tasks_for_sub(self, tasks, sub):
for m in sub.models_to_process:
Expand Down
21 changes: 18 additions & 3 deletions partfinder/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ def __init__(self, cfg):
self.cfg = cfg
self.path = os.path.join(self.cfg.subsets_path, 'data.db')
self.results = None
self.h5 = None

def _open(self):
assert self.h5 is None
if os.path.exists(self.path):
try:
self.h5 = tables.open_file(self.path, 'a')
Expand All @@ -143,26 +147,37 @@ def __init__(self, cfg):
f = tables.Filters(complib='blosc', complevel=5)
self.h5 = tables.open_file(self.path, 'w', filters=f)
self.results = self.h5.create_table(
'/', 'results', cfg.data_layout.data_type)
'/', 'results', self.cfg.data_layout.data_type)
self.results.cols.subset_id.create_csindex()

assert isinstance(self.results, tables.Table)
assert self.results.indexed

def get_results_for_subset(self, subset):
self._open()
conditions = {'current_id': subset.subset_id}
matching = self.results.read_where(
'subset_id == current_id', conditions)
self.close()
return matching

def is_empty(self):
return self.results.nrows == 0
self._open()
res = self.results.nrows == 0
self.close()
return res

def save_result(self, subset, n):
log.debug("begin saving result")
self._open()
# We have to take a slice here, as pytables can't handle single
# elements
self.results.append(subset.result_array[n:n+1])
self.cfg.database.results.flush()
self.close()
log.debug("done saving result")

def close(self):
self.h5.close()
if self.h5 is not None:
self.h5.close()
self.h5 = None

0 comments on commit c3d4546

Please sign in to comment.