From b7efdfd52c691aa54c5626131f7c1fc21e597a0a Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Mon, 29 Mar 2021 18:37:03 +0200 Subject: [PATCH] Slow down the DB write interval and catch the case that it changed during write --- backend/store.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/backend/store.py b/backend/store.py index 6aace3c0..a12787d9 100644 --- a/backend/store.py +++ b/backend/store.py @@ -247,11 +247,21 @@ class ChangeDetectionStore: def sync_to_json(self): print("Saving..") - with open(self.json_store_path, 'w') as json_file: - json.dump(self.__data, json_file, indent=4) - logging.info("Re-saved index") + data ={} - self.needs_write = False + try: + data = deepcopy(self.__data) + except RuntimeError: + time.sleep(0.5) + print ("! Data changed when writing to JSON, trying again..") + self.sync_to_json() + return + else: + with open(self.json_store_path, 'w') as json_file: + json.dump(data, json_file, indent=4) + logging.info("Re-saved index") + + self.needs_write = False # Thread runner, this helps with thread/write issues when there are many operations that want to update the JSON # by just running periodically in one thread, according to python, dict updates are threadsafe. @@ -263,6 +273,6 @@ class ChangeDetectionStore: return if self.needs_write: self.sync_to_json() - time.sleep(1) -# body of the constructor + time.sleep(3) +