1414def fetch_dbhandle (dbname_or_handle ):
1515 """
1616 Fetch a Database handle from dbname_or_handle.
17-
17+
1818 This function provides a unified way to get a Database instance. In parallel mode
1919 (Dask worker), it fetches the DBClient from the worker plugin to avoid serializing
2020 Database/DBClient objects. In serial mode, it returns the provided Database instance.
21-
21+
2222 :param dbname_or_handle: Either a string (database name) or a Database instance.
2323 If a string is provided, the function assumes it's being called in a Dask worker
2424 context and will get the DBClient from worker.data["dbclient"].
@@ -27,7 +27,7 @@ def fetch_dbhandle(dbname_or_handle):
2727 :raises: ValueError if dbname_or_handle is neither a string nor a Database instance,
2828 or if called with a string outside of a Dask worker context.
2929 """
30-
30+
3131 if isinstance (dbname_or_handle , str ):
3232 try :
3333 worker = get_worker ()
@@ -49,7 +49,9 @@ def fetch_dbhandle(dbname_or_handle):
4949 elif isinstance (dbname_or_handle , Database ):
5050 db = dbname_or_handle
5151 else :
52- message = "fetch_dbhandle: illegal type for arg1={}\n " .format (type (dbname_or_handle ))
52+ message = "fetch_dbhandle: illegal type for arg1={}\n " .format (
53+ type (dbname_or_handle )
54+ )
5355 message += "Must be str defining a db name or a Database object"
5456 raise ValueError (message )
5557 return db
@@ -64,7 +66,9 @@ class MongoDBWorker(WorkerPlugin):
6466 connection leaks when Database objects are serialized to workers.
6567 """
6668
67- def __init__ (self , dbname , url = "mongodb://localhost:27017/" , dbclient_key = "dbclient" ):
69+ def __init__ (
70+ self , dbname , url = "mongodb://localhost:27017/" , dbclient_key = "dbclient"
71+ ):
6872 self .dbname = dbname
6973 self .connection_url = url
7074 self .dbclient_key = dbclient_key
@@ -82,4 +86,3 @@ def teardown(self, worker):
8286 dbclient = worker .data .get (self .dbclient_key )
8387 if dbclient :
8488 dbclient .close ()
85-
0 commit comments