File tree 1 file changed +7
-1
lines changed
1 file changed +7
-1
lines changed Original file line number Diff line number Diff line change 21
21
import re
22
22
import sys
23
23
import warnings
24
+ import weakref
24
25
from typing import Any , Optional
25
26
26
27
from hopsworks_common import client , usage , util , version
@@ -351,6 +352,7 @@ def connect(self) -> None:
351
352
"""
352
353
client .stop ()
353
354
self ._connected = True
355
+ finalizer = weakref .finalize (self , self .close )
354
356
try :
355
357
# determine engine, needed to init client
356
358
if (self ._engine is not None and self ._engine .lower () == "spark" ) or (
@@ -413,6 +415,7 @@ def connect(self) -> None:
413
415
self ._provide_project ()
414
416
except (TypeError , ConnectionError ):
415
417
self ._connected = False
418
+ finalizer .detach ()
416
419
raise
417
420
418
421
self ._check_compatibility ()
@@ -446,7 +449,7 @@ def close(self) -> None:
446
449
This will clean up any materialized certificates on the local file system of
447
450
external environments such as AWS SageMaker.
448
451
449
- Usage is recommended but optional.
452
+ Usage is optional.
450
453
451
454
!!! example
452
455
```python
@@ -455,6 +458,9 @@ def close(self) -> None:
455
458
conn.close()
456
459
```
457
460
"""
461
+ if not self ._connected :
462
+ return # the connection is already closed
463
+
458
464
from hsfs import engine
459
465
460
466
if OpenSearchClientSingleton ._instance :
You can’t perform that action at this time.
0 commit comments