Код: Выделить всё
from beanie import init_beanie, Document
from pymongo import AsyncMongoClient
class Sample(Document):
name: str
async def init():
# Create Async PyMongo client
client = AsyncMongoClient(
"mongodb://user:pass@host:27017"
)
# Initialize beanie with the Sample document class and a database
await init_beanie(database=client.db_name, document_models=[Sample])
Код: Выделить всё
@asynccontextmanager
async def lifespan(app: FastAPI):
# Initialize mongodb connection and collections
app.state.mongodb_manager = await init_mongodb("database_name")
# Initialize all the other dbs
app.state.redis_manager = RedisManager()
app.state.weaviate_manager = WeaviateManager()
app.state.elasticsearch_manager = ElasticSearchManager()
yield
# Close all the connections at shutdown
await app.state.mongodb_manager.close_connection()
await app.state.redis_manager.close_connection()
app.state.weaviate_manager.close_connection()
app.state.elasticsearch_manager.close_connection()
iris_app = FastAPI(lifespan=lifespan)
Подробнее здесь: https://stackoverflow.com/questions/798 ... ekit-agent
Мобильная версия