fix async run

This commit is contained in:
Raphael Deem 2017-01-28 15:26:44 -08:00
parent 7257e5794f
commit 753d2da6db
2 changed files with 29 additions and 2 deletions

View File

@ -297,8 +297,8 @@ 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() loop = asyncio.get_event_loop()
asyncio.set_event_loop(loop) asyncio.set_event_loop_policy(async_loop.EventLoopPolicy())
if debug: if debug:
loop.set_debug(debug) loop.set_debug(debug)

27
tests/test_loop_policy.py Normal file
View File

@ -0,0 +1,27 @@
from sanic import Sanic
import asyncio
from signal import signal, SIGINT
import uvloop
def test_loop_policy():
app = Sanic('test_loop_policy')
server = app.create_server(host="0.0.0.0", port=8000)
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(server)
signal(SIGINT, lambda s, f: loop.close())
# serve() sets the event loop policy to uvloop but
# doesn't get called until we run the server task
assert isinstance(asyncio.get_event_loop_policy(),
asyncio.unix_events._UnixDefaultEventLoopPolicy)
try:
loop.run_until_complete(task)
except:
loop.stop()
assert isinstance(asyncio.get_event_loop_policy(),
uvloop.EventLoopPolicy)