From baa64f3dc5cc19327d9158a89b8f90b89ea55fd1 Mon Sep 17 00:00:00 2001 From: John Smith Date: Tue, 15 Oct 2024 23:12:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BF=A1=E5=8F=B7=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E5=8A=A0=E8=BD=BD=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index 799e90d..cb050e4 100755 --- a/main.py +++ b/main.py @@ -39,6 +39,7 @@ ROUTES = [ server: Optional[tornado.httpserver.HTTPServer] = None +cmd_args = None shut_down_event: Optional[asyncio.Event] = None @@ -55,11 +56,12 @@ async def main(): def init(): init_signal_handlers() - args = parse_args() + global cmd_args + cmd_args = parse_args() - init_logging(args.debug) + init_logging(cmd_args.debug) logger.info('App started, initializing') - config.init(args) + config.init(cmd_args) utils.request.init() models.database.init() @@ -83,21 +85,36 @@ def init_signal_handlers(): global shut_down_event shut_down_event = asyncio.Event() - signums = (signal.SIGINT, signal.SIGTERM) - try: - loop = asyncio.get_running_loop() - for signum in signums: - loop.add_signal_handler(signum, on_shut_down_signal) - except NotImplementedError: - # 不太安全,但Windows只能用这个 - for signum in signums: - signal.signal(signum, on_shut_down_signal) + is_win = sys.platform == 'win32' + loop = asyncio.get_running_loop() + if not is_win: + def add_signal_handler(signum, callback): + loop.add_signal_handler(signum, callback) + else: + def add_signal_handler(signum, callback): + # 不太安全,但Windows只能用这个 + signal.signal(signum, lambda _signum, _frame: loop.call_soon(callback)) + + shut_down_signums = (signal.SIGINT, signal.SIGTERM) + if not is_win: + reload_signum = signal.SIGHUP + else: + reload_signum = signal.SIGBREAK + + for shut_down_signum in shut_down_signums: + add_signal_handler(shut_down_signum, on_shut_down_signal) + add_signal_handler(reload_signum, on_reload_signal) -def on_shut_down_signal(*_args): +def on_shut_down_signal(): shut_down_event.set() +def on_reload_signal(): + logger.info('Received reload signal') + config.reload(cmd_args) + + def parse_args(): parser = argparse.ArgumentParser(description='用于OBS的仿YouTube风格的bilibili直播评论栏') parser.add_argument('--host', help='服务器host,默认和配置中的一样', default=None)