Mercurial > piecrust2
comparison 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 |
comparison
equal
deleted
inserted
replaced
1187:161cba5d031a | 1188:a7c43131d871 |
---|---|
269 cache_token = "%s@%s" % (source.name, content_item.spec) | 269 cache_token = "%s@%s" % (source.name, content_item.spec) |
270 cache_path = hashlib.md5(cache_token.encode('utf8')).hexdigest() + '.json' | 270 cache_path = hashlib.md5(cache_token.encode('utf8')).hexdigest() + '.json' |
271 page_time = source.getItemMtime(content_item) | 271 page_time = source.getItemMtime(content_item) |
272 if cache.isValid(cache_path, page_time): | 272 if cache.isValid(cache_path, page_time): |
273 try: | 273 try: |
274 cache_raw = cache.read(cache_path) | |
275 if not cache_raw: | |
276 for i in range(5): | |
277 cache_raw = cache.read(cache_path) | |
278 if cache_raw: | |
279 logger.warn("Had to re-pull the cache %d time(s)!" % (i + 1)) | |
280 break | |
281 if not cache_raw: | |
282 raise Exception("Cache is busted!") | |
283 | |
274 cache_data = json.loads( | 284 cache_data = json.loads( |
275 cache.read(cache_path), | 285 cache_raw, |
276 object_pairs_hook=collections.OrderedDict) | 286 object_pairs_hook=collections.OrderedDict) |
277 config = PageConfiguration( | 287 config = PageConfiguration( |
278 values=cache_data['config'], | 288 values=cache_data['config'], |
279 validate=False) | 289 validate=False) |
280 content = json_load_segments(cache_data['content']) | 290 content = json_load_segments(cache_data['content']) |