ruff
This commit is contained in:
committed by
Windpicker-owo
parent
83517b7178
commit
e02af208c4
@@ -1,7 +1,7 @@
|
||||
import asyncio
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
import asyncio
|
||||
from src.chat.planner_actions.action_manager import ChatterActionManager
|
||||
from src.common.data_models.message_manager_data_model import StreamContext
|
||||
from src.common.logger import get_logger
|
||||
|
||||
@@ -8,9 +8,8 @@ from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from src.chat.memory_system.integration_layer import IntegrationConfig, IntegrationMode, MemoryIntegrationLayer
|
||||
from src.chat.memory_system.memory_formatter import FormatterConfig, format_memories_for_llm
|
||||
|
||||
from src.chat.memory_system.memory_chunk import MemoryChunk, MemoryType
|
||||
from src.chat.memory_system.memory_formatter import FormatterConfig, format_memories_for_llm
|
||||
from src.common.logger import get_logger
|
||||
from src.llm_models.utils_model import LLMRequest
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ from datetime import datetime
|
||||
from typing import Any
|
||||
|
||||
from src.chat.memory_system.enhanced_memory_manager import enhanced_memory_manager
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config import global_config
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
from typing import Any
|
||||
|
||||
from src.chat.memory_system.enhanced_memory_hooks import enhanced_memory_hooks
|
||||
|
||||
from src.common.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -10,7 +10,6 @@ from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
from src.chat.memory_system.enhanced_memory_core import EnhancedMemorySystem
|
||||
|
||||
from src.chat.memory_system.memory_chunk import MemoryChunk
|
||||
from src.common.logger import get_logger
|
||||
from src.llm_models.utils_model import LLMRequest
|
||||
|
||||
@@ -12,7 +12,6 @@ from src.chat.memory_system.enhanced_memory_adapter import (
|
||||
process_conversation_with_enhanced_memory,
|
||||
retrieve_memories_with_enhanced_system,
|
||||
)
|
||||
|
||||
from src.common.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
@@ -9,8 +9,8 @@ from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
import orjson
|
||||
from src.chat.memory_system.enhanced_reranker import EnhancedReRanker, ReRankingConfig
|
||||
|
||||
from src.chat.memory_system.enhanced_reranker import EnhancedReRanker, ReRankingConfig
|
||||
from src.chat.memory_system.memory_chunk import MemoryChunk, MemoryType
|
||||
from src.common.logger import get_logger
|
||||
|
||||
|
||||
@@ -67,6 +67,12 @@ class StreamLoopManager:
|
||||
self.is_running = False
|
||||
|
||||
# 取消所有流循环
|
||||
try:
|
||||
# 使用带超时的锁获取,避免无限等待
|
||||
lock_acquired = await asyncio.wait_for(self.loop_lock.acquire(), timeout=10.0)
|
||||
if not lock_acquired:
|
||||
logger.error("停止管理器时获取锁超时")
|
||||
else:
|
||||
try:
|
||||
# 创建任务列表以便并发取消
|
||||
cancel_tasks = []
|
||||
@@ -85,6 +91,10 @@ class StreamLoopManager:
|
||||
|
||||
self.stream_loops.clear()
|
||||
logger.info("所有流循环已清理")
|
||||
finally:
|
||||
self.loop_lock.release()
|
||||
except asyncio.TimeoutError:
|
||||
logger.error("停止管理器时获取锁超时")
|
||||
except Exception as e:
|
||||
logger.error(f"停止管理器时出错: {e}")
|
||||
|
||||
@@ -183,9 +193,31 @@ class StreamLoopManager:
|
||||
except Exception as e:
|
||||
logger.error(f"等待流循环任务结束时出错: {stream_id} - {e}")
|
||||
|
||||
try:
|
||||
# 双重检查:在获取锁后再次检查流是否存在
|
||||
if stream_id not in self.stream_loops:
|
||||
logger.debug(f"流 {stream_id} 循环不存在(双重检查)")
|
||||
return False
|
||||
|
||||
task = self.stream_loops[stream_id]
|
||||
if not task.done():
|
||||
task.cancel()
|
||||
try:
|
||||
# 设置取消超时,避免无限等待
|
||||
await asyncio.wait_for(task, timeout=5.0)
|
||||
except asyncio.CancelledError:
|
||||
logger.debug(f"流循环任务已取消: {stream_id}")
|
||||
except asyncio.TimeoutError:
|
||||
logger.warning(f"流循环任务取消超时: {stream_id}")
|
||||
except Exception as e:
|
||||
logger.error(f"等待流循环任务结束时出错: {stream_id} - {e}")
|
||||
|
||||
del self.stream_loops[stream_id]
|
||||
logger.info(f"停止流循环: {stream_id} (剩余: {len(self.stream_loops)})")
|
||||
return True
|
||||
finally:
|
||||
# 确保锁被释放
|
||||
self.loop_lock.release()
|
||||
|
||||
async def _stream_loop(self, stream_id: str) -> None:
|
||||
"""单个流的无限循环
|
||||
|
||||
Reference in New Issue
Block a user