From e5196a49ff82d6f5ce22861d67b7ff6b835fb201 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 24 Jan 2017 12:14:06 +0900 Subject: [PATCH] `serve` closes the loop only when it created its own loop. When you were running multiple applications using asyncio, sanic must be closed as the latest order because it closes the loop. The patch limits the behavior only when sanic created its own loop by argument `loop=None`. --- sanic/server.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sanic/server.py b/sanic/server.py index a984854e..ab880f9b 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -290,8 +290,10 @@ def serve(host, port, request_handler, error_handler, before_start=None, :param protocol: Subclass of asyncio protocol class :return: Nothing """ - loop = loop or async_loop.new_event_loop() - asyncio.set_event_loop(loop) + create_new_loop = loop is None + if create_new_loop: + loop = async_loop.new_event_loop() + asyncio.set_event_loop(loop) if debug: loop.set_debug(debug) @@ -360,4 +362,5 @@ def serve(host, port, request_handler, error_handler, before_start=None, trigger_events(after_stop, loop) - loop.close() + if create_new_loop: + loop.close()