Extended buildSave to create a temp file and migrate that file to the live accessed file to prevent read/write race conditionns between threads

This commit is contained in:
Lyncoln
2026-10-24 21:04:20 -04:00
parent 7b949ec004
commit f21895e535
+29 -4
View File
@@ -1,5 +1,7 @@
import json
import sqlite3
import tempfile
import os
def buildSave(action, outputs):
if hasattr(action, "savedata"):
@@ -8,11 +10,34 @@ def buildSave(action, outputs):
for save_name in action.savedata:
save_data[save_name] = outputs[save_name]
with open(f"{action.savepath}", "w") as save_file:
json.dump(save_data, save_file)
#ith open(f"{action.savepath}", "w") as save_file:
# json.dump(save_data, save_file)
#print(f"Data saved by {action.name} to {action.savepath}")
return outputs
temporary_path = None
#directory = os.path.dirname(os.path.abspath(path))
directory = os.environ.get("TEMP_DIR")
try:
with tempfile.NamedTemporaryFile(
mode="w",
#dir=directory,
dir=directory,
delete=False
) as file:
json.dump(save_data, file)
temporary_path = file.name
os.replace(temporary_path, action.savepath)
finally:
if temporary_path:
try:
os.remove(temporary_path)
except FileNotFoundError:
pass
#print(f"Data saved by {action.name} to {action.savepath}")
return outputs
def loadSave(action, outputs):
if hasattr(action, "loaddata"):