Merge branch 'master' into ag_master

This commit is contained in:
Angus Hollands 2017-02-11 12:45:57 +00:00
commit 39720892ba

View File

@ -10,8 +10,8 @@ from urllib.parse import urlencode, urlunparse
from .config import Config from .config import Config
from .constants import HTTP_METHODS from .constants import HTTP_METHODS
from .handlers import ErrorHandler
from .exceptions import ServerError, URLBuildError from .exceptions import ServerError, URLBuildError
from .handlers import ErrorHandler
from .log import log from .log import log
from .response import HTTPResponse from .response import HTTPResponse
from .router import Router from .router import Router
@ -60,25 +60,31 @@ class Sanic:
# Decorator # Decorator
def before_start(self, func): def before_start(self, func):
"""Decorates a function to be executed before the server starts accepting connections""" """Decorates a function to be executed before the server starts
accepting connections
"""
self._before_start_callbacks.append(func) self._before_start_callbacks.append(func)
# Decorator # Decorator
def after_start(self, func): def after_start(self, func):
"""Decorates a function to be executed after the server starts accepting connections""" """Decorates a function to be executed after the server starts
accepting connections
"""
self._after_start_callbacks.append(func) self._after_start_callbacks.append(func)
# Decorator # Decorator
def after_stop(self, func): def after_stop(self, func):
"""Decorates a function to be executed when all requests are complete""" """Decorates a function to be executed when all requests are complete
"""
self._after_stop_callbacks.append(func) self._after_stop_callbacks.append(func)
# Decorator # Decorator
def before_stop(self, func): def before_stop(self, func):
"""Decorates a function to be executed when a stop signal is received before it is respected""" """Decorates a function to be executed when a stop signal is received
before it is respected
"""
self._before_stop_callbacks.append(func) self._before_stop_callbacks.append(func)
# Decorator # Decorator
def route(self, uri, methods=frozenset({'GET'}), host=None): def route(self, uri, methods=frozenset({'GET'}), host=None):
""" """
@ -414,9 +420,12 @@ class Sanic:
:param protocol: Subclass of asyncio protocol class :param protocol: Subclass of asyncio protocol class
:return: Nothing :return: Nothing
""" """
server_settings = self._helper(host=host, port=port, debug=debug, ssl=ssl, sock=sock, workers=workers, server_settings = self._helper(
loop=loop, protocol=protocol, backlog=backlog, stop_event=stop_event, host=host, port=port, debug=debug, ssl=ssl, sock=sock,
register_sys_signals=register_sys_signals) workers=workers, loop=loop, protocol=protocol, backlog=backlog,
stop_event=stop_event, register_sys_signals=register_sys_signals
)
try: try:
if workers == 1: if workers == 1:
serve(**server_settings) serve(**server_settings)
@ -431,13 +440,17 @@ class Sanic:
"""This kills the Sanic""" """This kills the Sanic"""
get_event_loop().stop() get_event_loop().stop()
async def create_server(self, host="127.0.0.1", port=8000, debug=False, ssl=None, sock=None, loop=None, async def create_server(self, host="127.0.0.1", port=8000, debug=False,
protocol=HttpProtocol, backlog=100, stop_event=None): ssl=None, sock=None, loop=None,
protocol=HttpProtocol, backlog=100,
stop_event=None):
""" """
Asynchronous version of `run`. Asynchronous version of `run`.
""" """
server_settings = self._helper(host=host, port=port, debug=debug, ssl=ssl, sock=sock, loop=loop, server_settings = self._helper(host=host, port=port, debug=debug,
protocol=protocol, backlog=backlog, stop_event=stop_event, run_async=True) ssl=ssl, sock=sock, loop=loop,
protocol=protocol, backlog=backlog,
stop_event=stop_event, run_async=True)
# Serve # Serve
proto = "http" proto = "http"
@ -447,8 +460,10 @@ class Sanic:
return await serve(**server_settings) return await serve(**server_settings)
def _helper(self, host="127.0.0.1", port=8000, debug=False, ssl=None, sock=None, workers=1, loop=None, def _helper(self, host="127.0.0.1", port=8000, debug=False, ssl=None,
protocol=HttpProtocol, backlog=100, stop_event=None, register_sys_signals=True, run_async=False): sock=None, workers=1, loop=None, protocol=HttpProtocol,
backlog=100, stop_event=None, register_sys_signals=True,
run_async=False):
""" """
Helper function used by `run` and `create_server`. Helper function used by `run` and `create_server`.
""" """
@ -486,10 +501,14 @@ class Sanic:
# -------------------------------------------- # # -------------------------------------------- #
for event_name, settings_name, args, reverse in ( for event_name, settings_name, args, reverse in (
("before_server_start", "before_start", self._before_start_callbacks, False), ("before_server_start", "before_start",
("after_server_start", "after_start", self._after_start_callbacks, False), self._before_start_callbacks, False),
("before_server_stop", "before_stop", self._before_stop_callbacks, True), ("after_server_start", "after_start",
("after_server_stop", "after_stop", self._after_stop_callbacks, True), self._after_start_callbacks, False),
("before_server_stop", "before_stop",
self._before_stop_callbacks, True),
("after_server_stop", "after_stop",
self._after_stop_callbacks, True),
): ):
listeners = [] listeners = []
for blueprint in self.blueprints.values(): for blueprint in self.blueprints.values():