Skip to content

Commit

Permalink
Move logging, shutdown tpex
Browse files Browse the repository at this point in the history
  • Loading branch information
WardLT authored Dec 2, 2023
1 parent da315aa commit 39f07ec
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
1 change: 0 additions & 1 deletion examol/store/db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def export_records(self, path: Path):
Args:
path: Path in which to save all data. Use a ".json.gz"
"""
logger.info(f'Started writing to {path}')
with (gzip.open(path, 'wt') if path.name.endswith('.gz') else open(path, 'w')) as fp:
for record in self.iterate_over_records():
print(record.json(), file=fp)
5 changes: 4 additions & 1 deletion examol/store/db/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, path: Path | None, write_freq: float = 10.):
self.db: dict[str, MoleculeRecord] = {}

# Start thread which writes until
self._thread_pool = ThreadPoolExecutor(max_workers=1)
self._thread_pool ThreadPoolExecutor | None = None
self._write_thread: Future | None = None
self._updates_available: Event = Event()
self._closing = Event()
Expand All @@ -43,6 +43,7 @@ def __init__(self, path: Path | None, write_freq: float = 10.):
def __enter__(self):
if self.path is not None:
logger.info('Start the writing thread')
self._thread_pool = ThreadPoolExecutor(max_workers=1)
self._write_thread = self._thread_pool.submit(self._writer)

# Add a callback to print a logging message if there is an error
Expand All @@ -64,6 +65,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
# Mark that we're closed
self._write_thread = None
self._closing.clear()
self._thread_pool.shutdown()

def _load_molecules(self):
"""Load molecules from disk"""
Expand Down Expand Up @@ -105,6 +107,7 @@ def _writer(self):

# Checkpoint and advance the standoff
temp_path = self.path.parent / (self.path.name + "-new")
logger.info(f'Started writing to {temp_path}')
self.export_records(temp_path)
move(temp_path, self.path)
next_write = monotonic() + self.write_freq
Expand Down

0 comments on commit 39f07ec

Please sign in to comment.