sanic/examples/run_async.py

23 lines
517 B
Python
Raw Permalink Normal View History

2017-01-24 03:58:37 +00:00
from sanic import Sanic
from sanic.response import json
from multiprocessing import Event
from signal import signal, SIGINT
import asyncio
2017-02-04 07:27:46 +00:00
import uvloop
2017-01-24 03:58:37 +00:00
app = Sanic(__name__)
@app.route("/")
async def test(request):
return json({"answer": "42"})
2017-02-04 07:27:46 +00:00
asyncio.set_event_loop(uvloop.new_event_loop())
2017-01-24 03:58:37 +00:00
server = app.create_server(host="0.0.0.0", port=8001)
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(server)
2017-02-04 07:27:46 +00:00
signal(SIGINT, lambda s, f: loop.stop())
2017-01-24 03:58:37 +00:00
try:
loop.run_forever()
except:
loop.stop()