added on_start on_stop
This commit is contained in:
parent
4489f536da
commit
d40e750ce6
|
@ -30,7 +30,7 @@ class Sanic:
|
||||||
|
|
||||||
return response
|
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.error_handler.debug=True
|
||||||
self.debug = debug
|
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)
|
|
@ -23,8 +23,6 @@ from .exceptions import ServerError
|
||||||
from .response import HTTPResponse
|
from .response import HTTPResponse
|
||||||
from .request import Request
|
from .request import Request
|
||||||
|
|
||||||
PRINT = 0
|
|
||||||
|
|
||||||
class HttpProtocol(asyncio.Protocol):
|
class HttpProtocol(asyncio.Protocol):
|
||||||
|
|
||||||
__slots__ = ('loop',
|
__slots__ = ('loop',
|
||||||
|
@ -158,7 +156,7 @@ def abort(msg):
|
||||||
log.info(msg, file=sys.stderr)
|
log.info(msg, file=sys.stderr)
|
||||||
sys.exit(1)
|
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
|
# Create Event Loop
|
||||||
loop = async_loop.new_event_loop()
|
loop = async_loop.new_event_loop()
|
||||||
asyncio.set_event_loop(loop)
|
asyncio.set_event_loop(loop)
|
||||||
|
@ -179,10 +177,21 @@ def serve(sanic, host, port, debug=False):
|
||||||
# Serve
|
# Serve
|
||||||
log.info('Goin\' Fast @ {}:{}'.format(host, port))
|
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_coroutine = loop.create_server(lambda: HttpProtocol(loop=loop, sanic=sanic), host, port)
|
||||||
server_loop = loop.run_until_complete(server_coroutine)
|
server_loop = loop.run_until_complete(server_coroutine)
|
||||||
try:
|
try:
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
finally:
|
finally:
|
||||||
|
if on_stop:
|
||||||
|
result = on_stop(sanic, loop)
|
||||||
|
if isawaitable(result):
|
||||||
|
loop.run_until_complete(result)
|
||||||
server_loop.close()
|
server_loop.close()
|
||||||
loop.close()
|
loop.close()
|
27
test.py
27
test.py
|
@ -1,27 +0,0 @@
|
||||||
from sanic import Sanic
|
|
||||||
from sanic.response import json, text
|
|
||||||
from sanic.exceptions import ServerError
|
|
||||||
|
|
||||||
app = Sanic("test")
|
|
||||||
|
|
||||||
@app.route("/")
|
|
||||||
async def test(request):
|
|
||||||
return json({ "test": True })
|
|
||||||
|
|
||||||
@app.route("/exception")
|
|
||||||
def test(request):
|
|
||||||
raise ServerError("yep")
|
|
||||||
|
|
||||||
@app.route("/exception/async")
|
|
||||||
async def test(request):
|
|
||||||
raise ServerError("asunk")
|
|
||||||
|
|
||||||
@app.route("/post_json")
|
|
||||||
def test(request):
|
|
||||||
return json({ "received": True, "message": request.json })
|
|
||||||
|
|
||||||
@app.route("/query_string")
|
|
||||||
def test(request):
|
|
||||||
return json({ "parsed": True, "args": request.args, "url": request.url, "query_string": request.query_string })
|
|
||||||
|
|
||||||
app.run(host="0.0.0.0")
|
|
83
tests/python.sanic.py
Normal file
83
tests/python.sanic.py
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
import asyncpg
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import inspect
|
||||||
|
|
||||||
|
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
|
||||||
|
parentdir = os.path.dirname(currentdir)
|
||||||
|
sys.path.insert(0,parentdir)
|
||||||
|
|
||||||
|
from sanic import Sanic
|
||||||
|
from sanic.response import json, text
|
||||||
|
from sanic.exceptions import ServerError
|
||||||
|
|
||||||
|
app = Sanic("test")
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
|
async def test(request):
|
||||||
|
return json({ "test": True })
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import asyncio_redis
|
||||||
|
import asyncpg
|
||||||
|
async def setup(sanic, loop):
|
||||||
|
sanic.conn = []
|
||||||
|
sanic.redis = []
|
||||||
|
for x in range(10):
|
||||||
|
sanic.conn.append(await asyncpg.connect(user='postgres', password='zomgdev', database='postgres', host='192.168.99.100'))
|
||||||
|
for n in range(30):
|
||||||
|
connection = await asyncio_redis.Connection.create(host='192.168.99.100', port=6379)
|
||||||
|
sanic.redis.append(connection)
|
||||||
|
|
||||||
|
|
||||||
|
c=0
|
||||||
|
@app.route("/postgres")
|
||||||
|
async def postgres(request):
|
||||||
|
global c
|
||||||
|
values = await app.conn[c].fetch('''SELECT * FROM players''')
|
||||||
|
c += 1
|
||||||
|
if c == 10:
|
||||||
|
c = 0
|
||||||
|
return text("yep")
|
||||||
|
|
||||||
|
r=0
|
||||||
|
@app.route("/redis")
|
||||||
|
async def redis(request):
|
||||||
|
global r
|
||||||
|
try:
|
||||||
|
values = await app.redis[r].get('my_key')
|
||||||
|
except asyncio_redis.exceptions.ConnectionLostError:
|
||||||
|
app.redis[r] = await asyncio_redis.Connection.create(host='127.0.0.1', port=6379)
|
||||||
|
values = await app.redis[r].get('my_key')
|
||||||
|
|
||||||
|
r += 1
|
||||||
|
if r == 30:
|
||||||
|
r = 0
|
||||||
|
return text(values)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/text")
|
||||||
|
def rtext(request):
|
||||||
|
return text("yeehaww")
|
||||||
|
|
||||||
|
@app.route("/exception")
|
||||||
|
def exception(request):
|
||||||
|
raise ServerError("yep")
|
||||||
|
|
||||||
|
@app.route("/exception/async")
|
||||||
|
async def test(request):
|
||||||
|
raise ServerError("asunk")
|
||||||
|
|
||||||
|
@app.route("/post_json")
|
||||||
|
def post_json(request):
|
||||||
|
return json({ "received": True, "message": request.json })
|
||||||
|
|
||||||
|
@app.route("/query_string")
|
||||||
|
def query_string(request):
|
||||||
|
return json({ "parsed": True, "args": request.args, "url": request.url, "query_string": request.query_string })
|
||||||
|
|
||||||
|
import sys
|
||||||
|
app.run(host="0.0.0.0", port=sys.argv[1])#, on_start=setup)
|
Loading…
Reference in New Issue
Block a user