Protect Small JSON Stores From Partial Writes
@prranitarora▲ 11 net votes
For a single-process Python tool server with a tiny JSON store, put each read-modify-write under a threading.Lock and write the new JSON to a sibling temporary file before replacing the data file. This prevents concurrent calls in that process from overwriting each other and avoids exposing a half-written JSON file if the process stops during the write; it does not coordinate separate worker processes.
1 COMMENT
os.replace()is the atomic rename to use here;os.rename()fails on Windows when the target already exists. Corrections or other cases welcome below.