view piecrust/commands/builtin/serving.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 fa3ee8a8ee2d
children 3ceeca7bb71c
line wrap: on
line source

import logging
from piecrust.commands.base import ChefCommand
from piecrust.serving.wrappers import run_werkzeug_server, run_gunicorn_server


logger = logging.getLogger(__name__)


class ServeCommand(ChefCommand):
    def __init__(self):
        super(ServeCommand, self).__init__()
        self.name = 'serve'
        self.description = "Runs a local web server to serve your website."
        self.cache_name = 'server'

    def setupParser(self, parser, app):
        parser.add_argument(
                '-p', '--port',
                help="The port for the web server",
                default=8080)
        parser.add_argument(
                '-a', '--address',
                help="The host for the web server",
                default='localhost')
        parser.add_argument(
                '--use-reloader',
                help="Restart the server when PieCrust code changes",
                action='store_true')
        parser.add_argument(
                '--use-debugger',
                help="Show the debugger when an error occurs",
                action='store_true')
        parser.add_argument(
                '--wsgi',
                help="The WSGI server implementation to use",
                choices=['werkzeug', 'gunicorn'],
                default='werkzeug')

    def run(self, ctx):
        root_dir = ctx.app.root_dir
        host = ctx.args.address
        port = int(ctx.args.port)
        debug = ctx.args.debug or ctx.args.use_debugger

        if ctx.args.wsgi == 'werkzeug':
            run_werkzeug_server(
                    root_dir, host, port,
                    debug_piecrust=debug,
                    sub_cache_dir=ctx.app.sub_cache_dir,
                    use_debugger=debug,
                    use_reloader=ctx.args.use_reloader)

        elif ctx.args.wsgi == 'gunicorn':
            options = {
                    'bind': '%s:%s' % (host, port),
                    'accesslog': '-',  # print access log to stderr
                    }
            if debug:
                options['loglevel'] = 'debug'
            if ctx.args.use_reloader:
                options['reload'] = True
            run_gunicorn_server(
                    root_dir,
                    debug_piecrust=debug,
                    sub_cache_dir=ctx.app.sub_cache_dir,
                    gunicorn_options=options)