diff piecrust/page.py @ 411:e7b865f8f335

bake: Enable multiprocess baking. Baking is now done by running a worker per CPU, and sending jobs to them. This changes several things across the codebase: * Ability to not cache things related to pages other than the 'main' page (i.e. the page at the bottom of the execution stack). * Decouple the baking process from the bake records, so only the main process keeps track (and modifies) the bake record. * Remove the need for 'batch page getters' and loading a page directly from the page factories. There are various smaller changes too included here, including support for scope performance timers that are saved with the bake record and can be printed out to the console. Yes I got carried away. For testing, the in-memory 'mock' file-system doesn't work anymore, since we're spawning processes, so this is replaced by a 'tmpfs' file-system which is saved in temporary files on disk and deleted after tests have run.
author Ludovic Chabant <ludovic@chabant.com>
date Fri, 12 Jun 2015 17:09:19 -0700
parents dd25bd3ce1f9
children 32c7c2d219d2
line wrap: on
line diff
--- a/piecrust/page.py	Sat May 30 15:41:52 2015 -0700
+++ b/piecrust/page.py	Fri Jun 12 17:09:19 2015 -0700
@@ -9,7 +9,8 @@
 import dateutil.parser
 import collections
 from werkzeug.utils import cached_property
-from piecrust.configuration import (Configuration, ConfigurationError,
+from piecrust.configuration import (
+        Configuration, ConfigurationError,
         parse_config_header)
 from piecrust.routing import IRouteMetadataProvider
 
@@ -241,9 +242,11 @@
 
 def load_page(app, path, path_mtime=None):
     try:
-        return _do_load_page(app, path, path_mtime)
+        with app.env.timerScope('PageLoad'):
+            return _do_load_page(app, path, path_mtime)
     except Exception as e:
-        logger.exception("Error loading page: %s" %
+        logger.exception(
+                "Error loading page: %s" %
                 os.path.relpath(path, app.root_dir))
         _, __, traceback = sys.exc_info()
         raise PageLoadingError(path, e).with_traceback(traceback)
@@ -255,9 +258,11 @@
     cache_path = hashlib.md5(path.encode('utf8')).hexdigest() + '.json'
     page_time = path_mtime or os.path.getmtime(path)
     if cache.isValid(cache_path, page_time):
-        cache_data = json.loads(cache.read(cache_path),
+        cache_data = json.loads(
+                cache.read(cache_path),
                 object_pairs_hook=collections.OrderedDict)
-        config = PageConfiguration(values=cache_data['config'],
+        config = PageConfiguration(
+                values=cache_data['config'],
                 validate=False)
         content = json_load_segments(cache_data['content'])
         return config, content, True
@@ -268,7 +273,7 @@
         raw = fp.read()
     header, offset = parse_config_header(raw)
 
-    if not 'format' in header:
+    if 'format' not in header:
         auto_formats = app.config.get('site/auto_formats')
         name, ext = os.path.splitext(path)
         header['format'] = auto_formats.get(ext, None)