diff piecrust/page.py @ 1188:a7c43131d871

bake: Fix file write flushing problem with Python 3.8+ Writing the cache files fails in Python 3.8 because it looks like flushing behaviour has changed. We need to explicitly flush. And even then, in very rare occurrences, it looks like it can still run into racing conditions, so we do a very hacky and ugly "retry" loop when fetching cached data :(
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 15 Jun 2021 22:36:23 -0700
parents e307f61d7034
children
line wrap: on
line diff
--- a/piecrust/page.py	Tue Jun 15 22:34:51 2021 -0700
+++ b/piecrust/page.py	Tue Jun 15 22:36:23 2021 -0700
@@ -271,8 +271,18 @@
     page_time = source.getItemMtime(content_item)
     if cache.isValid(cache_path, page_time):
         try:
+            cache_raw = cache.read(cache_path)
+            if not cache_raw:
+                for i in range(5):
+                    cache_raw = cache.read(cache_path)
+                    if cache_raw:
+                        logger.warn("Had to re-pull the cache %d time(s)!" % (i + 1))
+                        break
+                if not cache_raw:
+                    raise Exception("Cache is busted!")
+
             cache_data = json.loads(
-                cache.read(cache_path),
+                cache_raw,
                 object_pairs_hook=collections.OrderedDict)
             config = PageConfiguration(
                 values=cache_data['config'],