add async run

This commit is contained in:
Raphael Deem
2017-01-23 19:58:37 -08:00
parent 0f64702d72
commit 54ca8c787b
3 changed files with 83 additions and 5 deletions

20
examples/run_async.py Normal file
View File

@@ -0,0 +1,20 @@
from sanic import Sanic
from sanic.response import json
from multiprocessing import Event
from signal import signal, SIGINT
import asyncio
app = Sanic(__name__)
@app.route("/")
async def test(request):
return json({"answer": "42"})
server = app.create_server(host="0.0.0.0", port=8001)
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(server)
signal(SIGINT, lambda s, f: loop.close())
try:
loop.run_forever()
except:
loop.stop()