16
16
17
17
import os
18
18
import re
19
- import warnings
20
19
import sys
20
+ import warnings
21
+ import weakref
21
22
22
- from requests .exceptions import ConnectionError
23
-
24
- from hopsworks .decorators import connected , not_connected
25
23
from hopsworks import client , version
26
24
from hopsworks .core import project_api , secret_api , variable_api
25
+ from hopsworks .decorators import connected , not_connected
26
+ from requests .exceptions import ConnectionError
27
+
27
28
28
29
HOPSWORKS_PORT_DEFAULT = 443
29
30
HOSTNAME_VERIFICATION_DEFAULT = True
@@ -210,7 +211,8 @@ def _check_compatibility(self):
210
211
warnings .warn (
211
212
"The installed hopsworks client version {0} may not be compatible with the connected Hopsworks backend version {1}. \n To ensure compatibility please install the latest bug fix release matching the minor version of your backend ({2}) by running 'pip install hopsworks=={2}.*'" .format (
212
213
client_version , backend_version , major_minor_backend
213
- )
214
+ ),
215
+ stacklevel = 1 ,
214
216
)
215
217
sys .stderr .flush ()
216
218
@@ -241,6 +243,7 @@ def connect(self):
241
243
"""
242
244
client .stop ()
243
245
self ._connected = True
246
+ finalizer = weakref .finalize (self , self .close )
244
247
try :
245
248
# init client
246
249
if client .base .Client .REST_ENDPOINT not in os .environ :
@@ -263,6 +266,7 @@ def connect(self):
263
266
self ._variable_api = variable_api .VariableApi ()
264
267
except (TypeError , ConnectionError ):
265
268
self ._connected = False
269
+ finalizer .detach ()
266
270
raise
267
271
print (
268
272
"Connected. Call `.close()` to terminate connection gracefully." ,
@@ -278,7 +282,7 @@ def close(self):
278
282
This will clean up any materialized certificates on the local file system of
279
283
external environments such as AWS SageMaker.
280
284
281
- Usage is recommended but optional.
285
+ Usage is optional.
282
286
"""
283
287
from hsfs import client as hsfs_client
284
288
from hsfs import engine as hsfs_engine
@@ -299,6 +303,9 @@ def close(self):
299
303
except : # noqa: E722
300
304
pass
301
305
306
+ if not self ._connected :
307
+ return # the connection is already closed
308
+
302
309
client .stop ()
303
310
self ._connected = False
304
311
0 commit comments