Skip to content

Commit 67a8987

Browse files
jac0626silas.jiang
andauthored
enhance: improve AsyncMilvusClient connection error message (#3102)
Provide user-friendly error message with solutions and examples when AsyncMilvusClient is called from sync context without event loop. also see #3099 Signed-off-by: silas.jiang <[email protected]> Co-authored-by: silas.jiang <[email protected]>
1 parent 5f444ae commit 67a8987

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

pymilvus/milvus_client/_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import hashlib
33
import logging
44

5+
from pymilvus.exceptions import ConnectionConfigException
56
from pymilvus.orm.connections import connections
67

78
logger = logging.getLogger(__name__)
@@ -38,8 +39,12 @@ def create_connection(
3839
# reusing connections from closed event loops
3940
loop_id_fmt = ""
4041
if use_async:
41-
loop = asyncio.get_running_loop()
42-
loop_id_fmt = f"loop{id(loop)}"
42+
try:
43+
loop = asyncio.get_running_loop()
44+
loop_id_fmt = f"loop{id(loop)}"
45+
except RuntimeError as e:
46+
error_msg = "Cannot create async connection: no running event loop. Please ensure you are running in an async context."
47+
raise ConnectionConfigException(message=error_msg) from e
4348

4449
# different uri, auth, db_name, and event loop (for async) cannot share the same connection
4550
not_empty = [v for v in [use_async_fmt, uri, db_name, auth_fmt, loop_id_fmt] if v]

0 commit comments

Comments
 (0)