Skip to content

Revert "Enhance MilvusClient Initialization and Connection Management… #2880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions examples/loop_create_milvus_client.py

This file was deleted.

75 changes: 0 additions & 75 deletions examples/with_usage.py

This file was deleted.

6 changes: 3 additions & 3 deletions pymilvus/milvus_client/async_milvus_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
from typing import Dict, List, Optional, Union
from uuid import uuid4

from pymilvus.client.abstract import AnnSearchRequest, BaseRanker
from pymilvus.client.constants import DEFAULT_CONSISTENCY_LEVEL
Expand Down Expand Up @@ -826,8 +825,9 @@ def _create_connection(
**kwargs,
) -> str:
"""Create the connection to the Milvus server."""
# TODO: Implement reuse with new uri style
using = uuid4().hex
using = kwargs.pop("alias", None)
if not using or using == "":
using = f"async-{uri}{user}"
try:
connections.connect(
using, user, password, db_name, token, uri=uri, _async=True, **kwargs
Expand Down
54 changes: 7 additions & 47 deletions pymilvus/milvus_client/milvus_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import contextlib
import logging
from types import TracebackType
from typing import Dict, List, Optional, Type, Union
from typing import Dict, List, Optional, Union

from pymilvus.client.abstract import AnnSearchRequest, BaseRanker
from pymilvus.client.constants import DEFAULT_CONSISTENCY_LEVEL
Expand Down Expand Up @@ -63,30 +61,10 @@ def __init__(
to None.
Unit: second
"""
self._uri = uri
self._user = user
self._password = password
self._db_name = db_name
self._token = token
self._timeout = timeout
self._kwargs = kwargs
self._using = None
self._inited = False
self._init()

def _init(self):
if not self._inited:
self._using = self._create_connection(
self._uri,
self._user,
self._password,
self._db_name,
self._token,
timeout=self._timeout,
**self._kwargs,
)
self.is_self_hosted = bool(utility.get_server_type(using=self._using) == "milvus")
self._inited = True
self._using = self._create_connection(
uri, user, password, db_name, token, timeout=timeout, **kwargs
)
self.is_self_hosted = bool(utility.get_server_type(using=self._using) == "milvus")

def create_collection(
self,
Expand Down Expand Up @@ -934,26 +912,8 @@ def _create_collection_with_schema(
self.create_index(collection_name, index_params, timeout=timeout)
self.load_collection(collection_name, timeout=timeout)

def __enter__(self) -> "MilvusClient":
self._init()
return self

def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc: Optional[BaseException],
traceback: Optional[TracebackType],
) -> Optional[bool]:
self.close()

def __del__(self):
self.close()

def close(self):
if self._using is not None:
with contextlib.suppress(MilvusException):
connections.remove_connection(self._using)
self._using = None
connections.remove_connection(self._using)

def _get_connection(self):
return connections._fetch_handler(self._using)
Expand All @@ -969,7 +929,7 @@ def _create_connection(
) -> str:
"""Create the connection to the Milvus server."""
using = kwargs.pop("alias", None)
if using is None or using == "":
if not using or using == "":
using = f"{uri}{user}"
try:
connections.connect(using, user, password, db_name, token, uri=uri, **kwargs)
Expand Down
3 changes: 0 additions & 3 deletions pymilvus/orm/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,6 @@ def _fetch_handler(
self, alias: str = Config.MILVUS_CONN_ALIAS
) -> Union[GrpcHandler, AsyncGrpcHandler]:
"""Retrieves a GrpcHandler by alias."""
if alias is None:
raise ConnectionNotExistException(message=ExceptionsMessage.ConnectFirst)

if not isinstance(alias, str):
raise ConnectionConfigException(message=ExceptionsMessage.AliasType % type(alias))

Expand Down