From 753d2da6dbd0deb6f8cb2ceab7c93c8fba56acac Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Sat, 28 Jan 2017 15:26:44 -0800 Subject: [PATCH] fix async run --- sanic/server.py | 4 ++-- tests/test_loop_policy.py | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 tests/test_loop_policy.py diff --git a/sanic/server.py b/sanic/server.py index 48c3827e..be9bc31c 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -297,8 +297,8 @@ def serve(host, port, request_handler, error_handler, before_start=None, :param protocol: Subclass of asyncio protocol class :return: Nothing """ - loop = async_loop.new_event_loop() - asyncio.set_event_loop(loop) + loop = asyncio.get_event_loop() + asyncio.set_event_loop_policy(async_loop.EventLoopPolicy()) if debug: loop.set_debug(debug) diff --git a/tests/test_loop_policy.py b/tests/test_loop_policy.py new file mode 100644 index 00000000..f764548c --- /dev/null +++ b/tests/test_loop_policy.py @@ -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)