sanic/examples/run_async.py

30 lines
506 B
Python
Raw Normal View History

2017-01-24 03:58:37 +00:00
import asyncio
2017-02-04 07:27:46 +00:00
import uvloop
2017-01-24 03:58:37 +00:00
from sanic import Sanic, response
app = Sanic("Example")
2017-01-24 03:58:37 +00:00
2017-01-24 03:58:37 +00:00
@app.route("/")
async def test(request):
2017-04-06 19:39:54 +01:00
return response.json({"answer": "42"})
2017-01-24 03:58:37 +00:00
async def main():
server = await app.create_server(
port=8000, host="0.0.0.0", return_asyncio_server=True
)
if server is None:
return
await server.startup()
await server.serve_forever()
if __name__ == "__main__":
asyncio.set_event_loop(uvloop.new_event_loop())
asyncio.run(main())