From 7fcbbe2f029eb9a8375f895078d23c078f6712db Mon Sep 17 00:00:00 2001 From: gameloader Date: Mon, 13 Oct 2025 09:44:26 +0800 Subject: [PATCH] perf(memory): store memories asynchronously to prevent blocking --- memory_module/memory_integration.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/memory_module/memory_integration.py b/memory_module/memory_integration.py index 63cc1a0..fb4086a 100644 --- a/memory_module/memory_integration.py +++ b/memory_module/memory_integration.py @@ -126,7 +126,7 @@ In your response, consider the memories above to provide a personalized answer." assistant_response = response.choices[0].message.content - # Step 5: Store the interaction as new memories + # Step 5: Store the interaction as new memories (异步执行) messages = [ {"role": "user", "content": user_input}, {"role": "assistant", "content": assistant_response} @@ -138,7 +138,16 @@ In your response, consider the memories above to provide a personalized answer." "type": "chat_interaction" } - self.add_memory(messages, user_id, metadata) + # 异步存储记忆,不阻塞主流程 + def store_memory_async(): + try: + self.add_memory(messages, user_id, metadata) + except Exception as e: + print(f"[WARNING] Async memory storage failed: {e}") + + # 启动异步线程存储记忆 + memory_thread = threading.Thread(target=store_memory_async, daemon=True) + memory_thread.start() return { "success": True,