Browse Source

use an atomic operation to replace the cache with the new version

AUTOMATIC1111 2 years ago
parent
commit
bb7dd7b646
1 changed files with 4 additions and 1 deletions
  1. 4 1
      modules/cache.py

+ 4 - 1
modules/cache.py

@@ -30,9 +30,12 @@ def dump_cache():
             time.sleep(1)
 
         with cache_lock:
-            with open(cache_filename, "w", encoding="utf8") as file:
+            cache_filename_tmp = cache_filename + "-"
+            with open(cache_filename_tmp, "w", encoding="utf8") as file:
                 json.dump(cache_data, file, indent=4)
 
+            os.replace(cache_filename_tmp, cache_filename)
+
             dump_cache_after = None
             dump_cache_thread = None