From d134c850727e92881f8ef0376af5aeb3da3787af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A2=A8=E6=A2=93=E6=9F=92?= <1787882683@qq.com> Date: Sat, 22 Nov 2025 21:16:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=87=8D=E5=90=AF=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=BB=B6=E8=BF=9F=E9=87=8D?= =?UTF-8?q?=E5=90=AF=E4=BB=BB=E5=8A=A1=E4=BB=A5=E7=A1=AE=E4=BF=9D=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E5=B7=B2=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/webui/routers/system.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/webui/routers/system.py b/src/webui/routers/system.py index fb203f79..f826dc30 100644 --- a/src/webui/routers/system.py +++ b/src/webui/routers/system.py @@ -42,19 +42,23 @@ async def restart_maibot(): 使用 os.execv 重启当前进程,配置更改将在重启后生效。 注意:此操作会使麦麦暂时离线。 """ + import asyncio + try: # 记录重启操作 print(f"[{datetime.now()}] WebUI 触发重启操作") - # 使用 os.execv 重启当前进程 - # 这会替换当前进程,保持相同的 PID - python = sys.executable - args = [python] + sys.argv - - # 返回成功响应(实际上这个响应可能不会发送,因为进程会立即重启) - # 但我们仍然返回它以保持 API 一致性 - os.execv(python, args) + # 定义延迟重启的异步任务 + async def delayed_restart(): + await asyncio.sleep(0.5) # 延迟0.5秒,确保响应已发送 + python = sys.executable + args = [python] + sys.argv + os.execv(python, args) + + # 创建后台任务执行重启 + asyncio.create_task(delayed_restart()) + # 立即返回成功响应 return RestartResponse(success=True, message="麦麦正在重启中...") except Exception as e: raise HTTPException(status_code=500, detail=f"重启失败: {str(e)}") from e