added on_start on_stop

This commit is contained in:
Channel Cat
2016-10-05 05:30:36 +00:00
parent 4489f536da
commit d40e750ce6
6 changed files with 97 additions and 32 deletions

View File

@@ -30,7 +30,7 @@ class Sanic:
return response
def run(self, host="127.0.0.1", port=8000, debug=False):
def run(self, host="127.0.0.1", port=8000, debug=False, on_start=None, on_stop=None):
self.error_handler.debug=True
self.debug = debug
return serve(sanic=self, host=host, port=port, debug=debug)
return serve(sanic=self, host=host, port=port, debug=debug, on_start=on_start, on_stop=on_stop)

View File

@@ -23,8 +23,6 @@ from .exceptions import ServerError
from .response import HTTPResponse
from .request import Request
PRINT = 0
class HttpProtocol(asyncio.Protocol):
__slots__ = ('loop',
@@ -158,7 +156,7 @@ def abort(msg):
log.info(msg, file=sys.stderr)
sys.exit(1)
def serve(sanic, host, port, debug=False):
def serve(sanic, host, port, debug=False, on_start=None, on_stop=None):
# Create Event Loop
loop = async_loop.new_event_loop()
asyncio.set_event_loop(loop)
@@ -179,10 +177,21 @@ def serve(sanic, host, port, debug=False):
# Serve
log.info('Goin\' Fast @ {}:{}'.format(host, port))
if on_start:
print("start1")
result = on_start(sanic, loop)
if isawaitable(result):
print("start2")
loop.run_until_complete(result)
server_coroutine = loop.create_server(lambda: HttpProtocol(loop=loop, sanic=sanic), host, port)
server_loop = loop.run_until_complete(server_coroutine)
try:
loop.run_forever()
finally:
if on_stop:
result = on_stop(sanic, loop)
if isawaitable(result):
loop.run_until_complete(result)
server_loop.close()
loop.close()