From 7a7691cb9d5fcaabf7cc495d5582745deec7b8d7 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Sun, 16 Nov 2025 22:58:24 +0800 Subject: [PATCH] =?UTF-8?q?update:=E5=B0=9D=E8=AF=95=E4=BD=BF=E7=94=A8uvlo?= =?UTF-8?q?op=E5=8A=A0=E9=80=9F=E5=BC=82=E6=AD=A5IO=E6=80=A7=E8=83=BD?= =?UTF-8?q?=EF=BC=88=E4=BB=85Unix/macOS=E6=94=AF=E6=8C=81=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/app.py | 21 +++++++++++++++++++++ main/xiaozhi-server/requirements.txt | 3 ++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/main/xiaozhi-server/app.py b/main/xiaozhi-server/app.py index bc6f0b545..a47759b58 100644 --- a/main/xiaozhi-server/app.py +++ b/main/xiaozhi-server/app.py @@ -10,6 +10,18 @@ from core.http_server import SimpleHttpServer from core.websocket_server import WebSocketServer from core.utils.util import check_ffmpeg_installed +# 尝试使用uvloop加速异步IO性能(仅Unix/macOS支持) +# uvloop是asyncio的高性能替代品,性能提升2-4倍 +if sys.platform != "win32": + try: + import uvloop + asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) + _UVLOOP_ENABLED = True + except ImportError: + _UVLOOP_ENABLED = False +else: + _UVLOOP_ENABLED = False + TAG = __name__ logger = setup_logging() @@ -46,6 +58,15 @@ async def main(): check_ffmpeg_installed() config = load_config() + # 输出uvloop状态信息 + if _UVLOOP_ENABLED: + logger.bind(tag=TAG).info("✓ uvloop已启用,异步IO性能已优化") + else: + if sys.platform == "win32": + logger.bind(tag=TAG).warning("× uvloop不支持Windows平台,使用默认asyncio") + else: + logger.bind(tag=TAG).warning("× uvloop未安装,建议安装以提升性能:pip install uvloop") + # auth_key优先级:配置文件server.auth_key > manager-api.secret > 自动生成 # auth_key用于jwt认证,比如视觉分析接口的jwt认证、ota接口的token生成与websocket认证 # 获取配置文件中的auth_key diff --git a/main/xiaozhi-server/requirements.txt b/main/xiaozhi-server/requirements.txt index a5b791ff8..61fc1c76b 100644 --- a/main/xiaozhi-server/requirements.txt +++ b/main/xiaozhi-server/requirements.txt @@ -38,4 +38,5 @@ PyJWT==2.10.1 psutil==7.0.0 portalocker==3.2.0 Jinja2==3.1.6 -vosk==0.3.44 \ No newline at end of file +vosk==0.3.44 +uvloop==0.21.0; sys_platform != 'win32' \ No newline at end of file