diff --git a/sanic/sanic.py b/sanic/sanic.py index 56ea02ce..7198d854 100644 --- a/sanic/sanic.py +++ b/sanic/sanic.py @@ -169,7 +169,7 @@ class Sanic: # -------------------------------------------------------------------- # def run(self, host="127.0.0.1", port=8000, debug=False, after_start=None, - before_stop=None, sock=None, workers=1): + before_stop=None, sock=None, workers=1, loop=None): """ Runs the HTTP Server and listens until keyboard interrupt or term signal. On termination, drains connections before closing. @@ -183,6 +183,7 @@ class Sanic: :param sock: Socket for the server to accept connections from :param workers: Number of processes received before it is respected + :param loop: asyncio compatible event loop :return: Nothing """ self.error_handler.debug = True @@ -196,6 +197,7 @@ class Sanic: 'request_handler': self.handle_request, 'request_timeout': self.config.REQUEST_TIMEOUT, 'request_max_size': self.config.REQUEST_MAX_SIZE, + 'loop': loop } if debug: diff --git a/sanic/server.py b/sanic/server.py index 99032472..63563269 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -159,13 +159,29 @@ class HttpProtocol(asyncio.Protocol): def serve(host, port, request_handler, after_start=None, before_stop=None, debug=False, request_timeout=60, sock=None, - request_max_size=None, reuse_port=False): - # Create Event Loop - loop = async_loop.new_event_loop() + request_max_size=None, reuse_port=False, loop=None): + """ + Starts asynchronous HTTP Server on an individual process. + :param host: Address to host on + :param port: Port to host on + :param request_handler: Sanic request handler with middleware + :param after_start: Function to be executed after the server starts + listening. Takes single argument `loop` + :param before_stop: Function to be executed when a stop signal is + received before it is respected. Takes single argumenet `loop` + :param debug: Enables debug output (slows server) + :param request_timeout: time in seconds + :param sock: Socket for the server to accept connections from + :param request_max_size: size in bytes, `None` for no limit + :param reuse_port: `True` for multiple workers + :param loop: asyncio compatible event loop + :return: Nothing + """ + loop = loop or async_loop.new_event_loop() asyncio.set_event_loop(loop) - # I don't think we take advantage of this - # And it slows everything waaayyy down - # loop.set_debug(debug) + + if debug: + loop.set_debug(debug) connections = {} signal = Signal()