fix migration
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import asyncio
|
||||
import json
|
||||
import threading
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
@@ -24,6 +25,16 @@ class MemoryService:
|
||||
self.db = db
|
||||
self.user_id = user_id
|
||||
|
||||
@staticmethod
|
||||
def _run_async(coro):
|
||||
"""Run coroutine from sync code; safe inside FastAPI's running event loop."""
|
||||
try:
|
||||
asyncio.get_running_loop()
|
||||
except RuntimeError:
|
||||
return asyncio.run(coro)
|
||||
with ThreadPoolExecutor(max_workers=1) as pool:
|
||||
return pool.submit(asyncio.run, coro).result()
|
||||
|
||||
@staticmethod
|
||||
def _schedule_rag(coro) -> None:
|
||||
def runner() -> None:
|
||||
@@ -260,7 +271,7 @@ class MemoryService:
|
||||
return await retrieve_memory_facts(query or "", user_id=self.user_id, top_k=top_k)
|
||||
|
||||
try:
|
||||
rag_facts = asyncio.run(_load())
|
||||
rag_facts = self._run_async(_load())
|
||||
except Exception:
|
||||
rag_facts = []
|
||||
if rag_facts:
|
||||
|
||||
Reference in New Issue
Block a user