fix run_async demo

This commit is contained in:
Fengyuan Chen 2017-02-04 15:27:46 +08:00
parent 6f0b09509e
commit 884749f345
2 changed files with 6 additions and 3 deletions

View File

@ -3,6 +3,7 @@ from sanic.response import json
from multiprocessing import Event from multiprocessing import Event
from signal import signal, SIGINT from signal import signal, SIGINT
import asyncio import asyncio
import uvloop
app = Sanic(__name__) app = Sanic(__name__)
@ -10,10 +11,11 @@ app = Sanic(__name__)
async def test(request): async def test(request):
return json({"answer": "42"}) return json({"answer": "42"})
asyncio.set_event_loop(uvloop.new_event_loop())
server = app.create_server(host="0.0.0.0", port=8001) server = app.create_server(host="0.0.0.0", port=8001)
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
task = asyncio.ensure_future(server) task = asyncio.ensure_future(server)
signal(SIGINT, lambda s, f: loop.close()) signal(SIGINT, lambda s, f: loop.stop())
try: try:
loop.run_forever() loop.run_forever()
except: except:

View File

@ -297,8 +297,9 @@ def serve(host, port, request_handler, error_handler, before_start=None,
:param protocol: Subclass of asyncio protocol class :param protocol: Subclass of asyncio protocol class
:return: Nothing :return: Nothing
""" """
loop = async_loop.new_event_loop() if not run_async:
asyncio.set_event_loop(loop) loop = async_loop.new_event_loop()
asyncio.set_event_loop(loop)
if debug: if debug:
loop.set_debug(debug) loop.set_debug(debug)