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