1
- import contextlib
2
1
import logging
3
- from types import TracebackType
4
- from typing import Dict , List , Optional , Type , Union
2
+ from typing import Dict , List , Optional , Union
5
3
6
4
from pymilvus .client .abstract import AnnSearchRequest , BaseRanker
7
5
from pymilvus .client .constants import DEFAULT_CONSISTENCY_LEVEL
@@ -63,30 +61,10 @@ def __init__(
63
61
to None.
64
62
Unit: second
65
63
"""
66
- self ._uri = uri
67
- self ._user = user
68
- self ._password = password
69
- self ._db_name = db_name
70
- self ._token = token
71
- self ._timeout = timeout
72
- self ._kwargs = kwargs
73
- self ._using = None
74
- self ._inited = False
75
- self ._init ()
76
-
77
- def _init (self ):
78
- if not self ._inited :
79
- self ._using = self ._create_connection (
80
- self ._uri ,
81
- self ._user ,
82
- self ._password ,
83
- self ._db_name ,
84
- self ._token ,
85
- timeout = self ._timeout ,
86
- ** self ._kwargs ,
87
- )
88
- self .is_self_hosted = bool (utility .get_server_type (using = self ._using ) == "milvus" )
89
- self ._inited = True
64
+ self ._using = self ._create_connection (
65
+ uri , user , password , db_name , token , timeout = timeout , ** kwargs
66
+ )
67
+ self .is_self_hosted = bool (utility .get_server_type (using = self ._using ) == "milvus" )
90
68
91
69
def create_collection (
92
70
self ,
@@ -934,26 +912,8 @@ def _create_collection_with_schema(
934
912
self .create_index (collection_name , index_params , timeout = timeout )
935
913
self .load_collection (collection_name , timeout = timeout )
936
914
937
- def __enter__ (self ) -> "MilvusClient" :
938
- self ._init ()
939
- return self
940
-
941
- def __exit__ (
942
- self ,
943
- exc_type : Optional [Type [BaseException ]],
944
- exc : Optional [BaseException ],
945
- traceback : Optional [TracebackType ],
946
- ) -> Optional [bool ]:
947
- self .close ()
948
-
949
- def __del__ (self ):
950
- self .close ()
951
-
952
915
def close (self ):
953
- if self ._using is not None :
954
- with contextlib .suppress (MilvusException ):
955
- connections .remove_connection (self ._using )
956
- self ._using = None
916
+ connections .remove_connection (self ._using )
957
917
958
918
def _get_connection (self ):
959
919
return connections ._fetch_handler (self ._using )
@@ -969,7 +929,7 @@ def _create_connection(
969
929
) -> str :
970
930
"""Create the connection to the Milvus server."""
971
931
using = kwargs .pop ("alias" , None )
972
- if using is None or using == "" :
932
+ if not using or using == "" :
973
933
using = f"{ uri } { user } "
974
934
try :
975
935
connections .connect (using , user , password , db_name , token , uri = uri , ** kwargs )
0 commit comments