From 53d0059848af74836f939618dfa73c1b75739bf3 Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Mon, 19 Feb 2024 19:27:42 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=B8=8D=E5=86=8D=E9=9C=80=E8=A6=81exi?= =?UTF-8?q?t=E6=9D=A5=E9=80=80=E5=87=BA=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/core/app.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/pkg/core/app.py b/pkg/core/app.py index 2344c346..ab483448 100644 --- a/pkg/core/app.py +++ b/pkg/core/app.py @@ -79,18 +79,30 @@ class Application: asyncio.create_task(self.ctrl.run()) ] - async def interrupt(tasks): - await asyncio.sleep(1.5) - while await aioconsole.ainput("使用 ctrl+c 或 'exit' 退出程序 > ") != 'exit': - pass + # async def interrupt(tasks): + # await asyncio.sleep(1.5) + # while await aioconsole.ainput("使用 ctrl+c 或 'exit' 退出程序 > ") != 'exit': + # pass + # for task in tasks: + # task.cancel() + + # await interrupt(tasks) + + import signal + + def signal_handler(sig, frame): for task in tasks: task.cancel() - - await interrupt(tasks) + self.logger.info("程序退出.") + exit(0) + + signal.signal(signal.SIGINT, signal_handler) + + await asyncio.gather(*tasks, return_exceptions=True) except asyncio.CancelledError: pass except Exception as e: self.logger.error(f"应用运行致命异常: {e}") self.logger.debug(f"Traceback: {traceback.format_exc()}") - self.logger.info("程序退出.") +