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:
@@ -1,5 +1,7 @@
|
||||
import json
|
||||
import sqlite3
|
||||
import tempfile
|
||||
import os
|
||||
|
||||
def buildSave(action, outputs):
|
||||
if hasattr(action, "savedata"):
|
||||
@@ -8,8 +10,31 @@ 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)
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user