From e5c32e9b4818f2f9c42049d966b080038b0c27dd Mon Sep 17 00:00:00 2001 From: "(Zenix) Han-Sheng Huang" Date: Thu, 2 Mar 2017 13:27:52 +0900 Subject: [PATCH 1/2] special handling when sock is provided and number of workers > 1 --- sanic/server.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/sanic/server.py b/sanic/server.py index 83bda3fc..209a5e45 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -396,13 +396,15 @@ def serve_multiple(server_settings, workers, stop_event=None): " has more information.", DeprecationWarning) server_settings['reuse_port'] = True - sock = socket() - sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) - sock.bind((server_settings['host'], server_settings['port'])) - set_inheritable(sock.fileno(), True) - server_settings['sock'] = sock - server_settings['host'] = None - server_settings['port'] = None + # Handling when custom socket is not provided. + if server_settings.get('sock') == None: + sock = socket() + sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) + sock.bind((server_settings['host'], server_settings['port'])) + set_inheritable(sock.fileno(), True) + server_settings['sock'] = sock + server_settings['host'] = None + server_settings['port'] = None if stop_event is None: stop_event = Event() @@ -423,6 +425,6 @@ def serve_multiple(server_settings, workers, stop_event=None): # the above processes will block this until they're stopped for process in processes: process.terminate() - sock.close() + server_settings.get('sock').close() asyncio.get_event_loop().stop() From 62bf213a6eef5fcd44179a9758dcc44f07453350 Mon Sep 17 00:00:00 2001 From: "(Zenix) Han-Sheng Huang" Date: Thu, 2 Mar 2017 13:42:55 +0900 Subject: [PATCH 2/2] pass flake8 --- sanic/server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/server.py b/sanic/server.py index 209a5e45..28ce0848 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -397,7 +397,7 @@ def serve_multiple(server_settings, workers, stop_event=None): server_settings['reuse_port'] = True # Handling when custom socket is not provided. - if server_settings.get('sock') == None: + if server_settings.get('sock') is None: sock = socket() sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind((server_settings['host'], server_settings['port']))