修复退出机制

修复wait_for_user_input()中使用了shutdown_event.wait(),但没有触发set()
This commit is contained in:
LuiKlee
2025-11-27 19:21:27 +08:00
committed by GitHub
parent ee39ba0fd2
commit bea0d033cf

10
bot.py
View File

@@ -249,7 +249,7 @@ class ShutdownManager:
return success return success
except Exception as e: except Exception as e:
logger.error(f"麦麦关闭失败: {e}") logger.error(f"麦麦关闭失败: {e}", exc_info=True)
return False return False
@@ -602,12 +602,11 @@ class MaiBotMain:
async def wait_for_user_input(): async def wait_for_user_input():
"""等待用户输入(异步方式)""" """等待用户输入(异步方式)"""
try: try:
# 在非生产环境下,使用异步方式等待输入
if os.getenv("ENVIRONMENT") != "production": if os.getenv("ENVIRONMENT") != "production":
logger.info("程序执行完成,按 Ctrl+C 退出...") logger.info("程序执行完成,按 Ctrl+C 退出...")
# 使用 Event 替代 sleep 循环,避免阻塞事件循环 # 使用非阻塞循环
shutdown_event = asyncio.Event() while True:
await shutdown_event.wait() await asyncio.sleep(0.1)
except KeyboardInterrupt: except KeyboardInterrupt:
logger.info("用户中断程序") logger.info("用户中断程序")
return True return True
@@ -616,6 +615,7 @@ async def wait_for_user_input():
return False return False
async def main_async(): async def main_async():
"""主异步函数""" """主异步函数"""
exit_code = 0 exit_code = 0