From 884749f345b951c7bc4d8d01dcfb405df6999f2e Mon Sep 17 00:00:00 2001 From: Fengyuan Chen Date: Sat, 4 Feb 2017 15:27:46 +0800 Subject: [PATCH] fix run_async demo --- examples/run_async.py | 4 +++- sanic/server.py | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/run_async.py b/examples/run_async.py index 71912e5a..d514c7d0 100644 --- a/examples/run_async.py +++ b/examples/run_async.py @@ -3,6 +3,7 @@ from sanic.response import json from multiprocessing import Event from signal import signal, SIGINT import asyncio +import uvloop app = Sanic(__name__) @@ -10,10 +11,11 @@ app = Sanic(__name__) async def test(request): return json({"answer": "42"}) +asyncio.set_event_loop(uvloop.new_event_loop()) server = app.create_server(host="0.0.0.0", port=8001) loop = asyncio.get_event_loop() task = asyncio.ensure_future(server) -signal(SIGINT, lambda s, f: loop.close()) +signal(SIGINT, lambda s, f: loop.stop()) try: loop.run_forever() except: diff --git a/sanic/server.py b/sanic/server.py index 48c3827e..8b9ed532 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -297,8 +297,9 @@ 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) + if not run_async: + loop = async_loop.new_event_loop() + asyncio.set_event_loop(loop) if debug: loop.set_debug(debug)