From ee18fbebae163695ee8764a38e661c36a4ba17b5 Mon Sep 17 00:00:00 2001 From: CGD <3030332422@qq.com> Date: Wed, 7 May 2025 16:20:43 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E2=80=9C?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E8=BF=90=E8=A1=8C=E6=97=B6=E6=9C=AA?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=A0=87=E5=87=86=E8=BE=93=E5=85=A5=EF=BC=88?= =?UTF-8?q?stdin=EF=BC=89=EF=BC=8C=E5=AF=BC=E8=87=B4=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E8=A2=AB=E7=BC=93=E5=86=B2=EF=BC=8C=E7=9B=B4=E5=88=B0=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E7=BB=88=E6=AD=A2=E5=90=8E=E6=89=8D=E9=87=8A=E6=94=BE?= =?UTF-8?q?=E2=80=9D=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/app.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/main/xiaozhi-server/app.py b/main/xiaozhi-server/app.py index 4ad57855..619fb5c7 100644 --- a/main/xiaozhi-server/app.py +++ b/main/xiaozhi-server/app.py @@ -7,6 +7,7 @@ from core.ota_server import SimpleOtaServer from core.utils.util import check_ffmpeg_installed from config.logger import setup_logging from core.utils.util import get_local_ip +from aioconsole import ainput TAG = __name__ logger = setup_logging() @@ -34,10 +35,19 @@ async def wait_for_exit() -> None: pass +async def monitor_stdin(): + """监控标准输入,消费回车键""" + while True: + await ainput() # 异步等待输入,消费回车 + + async def main(): check_ffmpeg_installed() config = load_config() + # 添加 stdin 监控任务 + stdin_task = asyncio.create_task(monitor_stdin()) + # 启动 WebSocket 服务器 ws_server = WebSocketServer(config) ws_task = asyncio.create_task(ws_server.start()) @@ -78,19 +88,22 @@ async def main(): ) try: - await wait_for_exit() # 监听退出信号 + await wait_for_exit() # 阻塞直到收到退出信号 except asyncio.CancelledError: print("任务被取消,清理资源中...") finally: + # 取消所有任务(关键修复点) + stdin_task.cancel() ws_task.cancel() if ota_task: ota_task.cancel() - try: - await ws_task - if ota_task: - await ota_task - except asyncio.CancelledError: - pass + + # 等待任务终止(必须加超时) + await asyncio.wait( + [stdin_task, ws_task, ota_task] if ota_task else [stdin_task, ws_task], + timeout=3.0, + return_when=asyncio.ALL_COMPLETED + ) print("服务器已关闭,程序退出。")