From dc4827ccb255021b6c65cc9b3da4fd04b9c02dbc Mon Sep 17 00:00:00 2001 From: kalicyh <34980061+kaliCYH@users.noreply.github.com> Date: Wed, 12 Mar 2025 10:24:39 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=88=20perf:=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=96=87=E6=A1=A3+=E4=BC=98=E9=9B=85=E7=9A=84=E9=80=80?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/Deployment.md | 22 +++------------------- main/xiaozhi-server/app.py | 33 +++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/docs/Deployment.md b/docs/Deployment.md index 0dfd93b51..94b6e363a 100644 --- a/docs/Deployment.md +++ b/docs/Deployment.md @@ -117,7 +117,7 @@ docker run -it --name xiaozhi-env --restart always --security-opt seccomp:unconf -p 8000:8000 \ -p 8002:8002 \ -v ./:/app \ - kalicyh/poetry:v3.10_xiaozhi + kalicyh/python:xiaozhi ``` 然后就和正常开发一样了 @@ -127,29 +127,13 @@ docker run -it --name xiaozhi-env --restart always --security-opt seccomp:unconf 在刚刚的打开的终端运行 ```sh -poetry install --no-root -``` - -```sh -apt-get update -apt-get install -y --no-install-recommends libopus0 ffmpeg -``` - -速度慢可以尝试使用清华镜像 - -```sh -echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware" > /etc/apt/sources.list -echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware" >> /etc/apt/sources.list -echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware" >> /etc/apt/sources.list -echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware" >> /etc/apt/sources.list -apt-get update -apt-get install -y --no-install-recommends libopus0 ffmpeg +pip install -r requirements.txt ``` ## 6.运行项目 ```sh -poetry run python app.py +python app.py ``` # 方式三:本地源码运行 diff --git a/main/xiaozhi-server/app.py b/main/xiaozhi-server/app.py index bd23c7bcd..ca3ab5453 100644 --- a/main/xiaozhi-server/app.py +++ b/main/xiaozhi-server/app.py @@ -1,10 +1,27 @@ import asyncio +import sys +import signal from config.settings import load_config, check_config_file from core.websocket_server import WebSocketServer from core.utils.util import check_ffmpeg_installed TAG = __name__ +async def wait_for_exit(): + """Windows 和 Linux 兼容的退出监听""" + loop = asyncio.get_running_loop() + stop_event = asyncio.Event() + + if sys.platform == "win32": + # Windows: 用 sys.stdin.read() 监听 Ctrl + C + await loop.run_in_executor(None, sys.stdin.read) + else: + # Linux/macOS: 用 signal 监听 Ctrl + C + def stop(): + stop_event.set() + loop.add_signal_handler(signal.SIGINT, stop) + loop.add_signal_handler(signal.SIGTERM, stop) # 支持 kill 进程 + await stop_event.wait() async def main(): check_config_file() @@ -16,11 +33,19 @@ async def main(): ws_task = asyncio.create_task(ws_server.start()) try: - # 等待 WebSocket 服务器运行 - await ws_task + await wait_for_exit() # 监听退出信号 + except asyncio.CancelledError: + print("任务被取消,清理资源中...") finally: ws_task.cancel() - + try: + await ws_task + except asyncio.CancelledError: + pass + print("服务器已关闭,程序退出。") if __name__ == "__main__": - asyncio.run(main()) + try: + asyncio.run(main()) + except KeyboardInterrupt: + print("手动中断,程序终止。")