Exposes loopin sanic serve and run functions (#64)

This commit is contained in:
Blake VandeMerwe 2016-10-18 10:05:29 -06:00
parent 452438dc07
commit 5e459cb69d
2 changed files with 25 additions and 7 deletions

View File

@ -170,7 +170,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.
@ -184,6 +184,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
@ -197,6 +198,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:

View File

@ -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()