signal handling
This commit is contained in:
parent
bd7333723e
commit
11017902be
|
@ -20,9 +20,10 @@ from httptools import HttpRequestParser
|
||||||
from httptools.parser.errors import HttpParserError
|
from httptools.parser.errors import HttpParserError
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import uvloop as async_loop
|
import uvloop
|
||||||
|
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
|
||||||
except ImportError:
|
except ImportError:
|
||||||
async_loop = asyncio
|
pass
|
||||||
|
|
||||||
from sanic.log import logger, access_logger
|
from sanic.log import logger, access_logger
|
||||||
from sanic.response import HTTPResponse
|
from sanic.response import HTTPResponse
|
||||||
|
@ -509,11 +510,11 @@ def serve(host, port, request_handler, error_handler, before_start=None,
|
||||||
request_timeout=60, response_timeout=60, keep_alive_timeout=5,
|
request_timeout=60, response_timeout=60, keep_alive_timeout=5,
|
||||||
ssl=None, sock=None, request_max_size=None, reuse_port=False,
|
ssl=None, sock=None, request_max_size=None, reuse_port=False,
|
||||||
loop=None, protocol=HttpProtocol, backlog=100,
|
loop=None, protocol=HttpProtocol, backlog=100,
|
||||||
register_sys_signals=True, run_async=False, connections=None,
|
register_sys_signals=True, run_multiple=False, run_async=False,
|
||||||
signal=Signal(), request_class=None, access_log=True,
|
connections=None, signal=Signal(), request_class=None,
|
||||||
keep_alive=True, is_request_stream=False, router=None,
|
access_log=True, keep_alive=True, is_request_stream=False,
|
||||||
websocket_max_size=None, websocket_max_queue=None, state=None,
|
router=None, websocket_max_size=None, websocket_max_queue=None,
|
||||||
graceful_shutdown_timeout=15.0):
|
state=None, graceful_shutdown_timeout=15.0):
|
||||||
"""Start asynchronous HTTP Server on an individual process.
|
"""Start asynchronous HTTP Server on an individual process.
|
||||||
|
|
||||||
:param host: Address to host on
|
:param host: Address to host on
|
||||||
|
@ -547,7 +548,10 @@ def serve(host, port, request_handler, error_handler, before_start=None,
|
||||||
:return: Nothing
|
:return: Nothing
|
||||||
"""
|
"""
|
||||||
if not run_async:
|
if not run_async:
|
||||||
loop = async_loop.new_event_loop()
|
# create new event_loop after fork
|
||||||
|
asyncio.get_event_loop().close()
|
||||||
|
|
||||||
|
loop = asyncio.new_event_loop()
|
||||||
asyncio.set_event_loop(loop)
|
asyncio.set_event_loop(loop)
|
||||||
|
|
||||||
if debug:
|
if debug:
|
||||||
|
@ -605,7 +609,8 @@ def serve(host, port, request_handler, error_handler, before_start=None,
|
||||||
|
|
||||||
# Register signals for graceful termination
|
# Register signals for graceful termination
|
||||||
if register_sys_signals:
|
if register_sys_signals:
|
||||||
for _signal in (SIGINT, SIGTERM):
|
_singals = (SIGTERM,) if run_multiple else (SIGINT, SIGTERM)
|
||||||
|
for _signal in _singals:
|
||||||
try:
|
try:
|
||||||
loop.add_signal_handler(_signal, loop.stop)
|
loop.add_signal_handler(_signal, loop.stop)
|
||||||
except NotImplementedError:
|
except NotImplementedError:
|
||||||
|
@ -668,6 +673,7 @@ def serve_multiple(server_settings, workers):
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
server_settings['reuse_port'] = True
|
server_settings['reuse_port'] = True
|
||||||
|
server_settings['run_multiple'] = True
|
||||||
|
|
||||||
# Handling when custom socket is not provided.
|
# Handling when custom socket is not provided.
|
||||||
if server_settings.get('sock') is None:
|
if server_settings.get('sock') is None:
|
||||||
|
@ -682,7 +688,7 @@ def serve_multiple(server_settings, workers):
|
||||||
def sig_handler(signal, frame):
|
def sig_handler(signal, frame):
|
||||||
logger.info("Received signal %s. Shutting down.", Signals(signal).name)
|
logger.info("Received signal %s. Shutting down.", Signals(signal).name)
|
||||||
for process in processes:
|
for process in processes:
|
||||||
os.kill(process.pid, SIGINT)
|
os.kill(process.pid, SIGTERM)
|
||||||
|
|
||||||
signal_func(SIGINT, lambda s, f: sig_handler(s, f))
|
signal_func(SIGINT, lambda s, f: sig_handler(s, f))
|
||||||
signal_func(SIGTERM, lambda s, f: sig_handler(s, f))
|
signal_func(SIGTERM, lambda s, f: sig_handler(s, f))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user