diff piecrust/rendering.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 4b1019bb2533
children 0e9a94b7fdfa
line wrap: on
line diff
--- a/piecrust/rendering.py	Sat May 30 15:41:52 2015 -0700
+++ b/piecrust/rendering.py	Fri Jun 12 17:09:19 2015 -0700
@@ -2,8 +2,8 @@
 import os.path
 import logging
 from werkzeug.utils import cached_property
-from piecrust.data.builder import (DataBuildingContext, build_page_data,
-        build_layout_data)
+from piecrust.data.builder import (
+        DataBuildingContext, build_page_data, build_layout_data)
 from piecrust.data.filters import (
         PaginationFilter, HasFilterClause, IsFilterClause, AndBooleanClause,
         page_value_accessor)
@@ -94,6 +94,12 @@
         return self.page.getUri(self.page_num)
 
     @property
+    def pagination_has_more(self):
+        if self.used_pagination is None:
+            return False
+        return self.used_pagination.has_more
+
+    @property
     def current_pass_info(self):
         return self.render_passes.get(self._current_pass)
 
@@ -157,15 +163,19 @@
         # Render content segments.
         ctx.setCurrentPass(PASS_FORMATTING)
         repo = ctx.app.env.rendered_segments_repository
+        save_to_fs = True
+        if ctx.app.env.fs_cache_only_for_main_page and not eis.is_main_page:
+            save_to_fs = False
         if repo and not ctx.force_render:
-            cache_key = ctx.uri
-            page_time = page.path_mtime
             contents = repo.get(
-                    cache_key,
+                    ctx.uri,
                     lambda: _do_render_page_segments(page, page_data),
-                    fs_cache_time=page_time)
+                    fs_cache_time=page.path_mtime,
+                    save_to_fs=save_to_fs)
         else:
             contents = _do_render_page_segments(page, page_data)
+            if repo:
+                repo.put(ctx.uri, contents, save_to_fs)
 
         # Render layout.
         ctx.setCurrentPass(PASS_RENDERING)
@@ -226,9 +236,10 @@
         for seg_part in seg.parts:
             part_format = seg_part.fmt or format_name
             try:
-                part_text = engine.renderString(
-                        seg_part.content, page_data,
-                        filename=page.path)
+                with app.env.timerScope(engine.__class__.__name__):
+                    part_text = engine.renderString(
+                            seg_part.content, page_data,
+                            filename=page.path)
             except TemplatingError as err:
                 err.lineno += seg_part.line
                 raise err
@@ -291,7 +302,8 @@
     format_name = format_name or app.config.get('site/default_format')
     for fmt in app.plugin_loader.getFormatters():
         if fmt.FORMAT_NAMES is None or format_name in fmt.FORMAT_NAMES:
-            txt = fmt.render(format_name, txt)
+            with app.env.timerScope(fmt.__class__.__name__):
+                txt = fmt.render(format_name, txt)
             format_count += 1
             if fmt.OUTPUT_FORMAT is not None:
                 format_name = fmt.OUTPUT_FORMAT