Skip to content

Commit

Permalink
Add finalizer to Connection (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
aversey authored Oct 22, 2024
1 parent bdcdf69 commit d5c3ff0
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion python/hsml/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#

import os
import weakref

from hsml import client
from hsml.core import model_api, model_registry_api, model_serving_api
Expand Down Expand Up @@ -165,6 +166,7 @@ def connect(self):
```
"""
self._connected = True
finalizer = weakref.finalize(self, self.close)
try:
# init client
if client.hopsworks.base.Client.REST_ENDPOINT not in os.environ:
Expand All @@ -185,6 +187,7 @@ def connect(self):
self._model_serving_api.load_default_configuration() # istio client, default resources,...
except (TypeError, ConnectionError):
self._connected = False
finalizer.detach()
raise
print("Connected. Call `.close()` to terminate connection gracefully.")

Expand All @@ -194,8 +197,10 @@ def close(self):
This will clean up any materialized certificates on the local file system of
external environments.
Usage is recommended but optional.
Usage is optional.
"""
if not self._connected:
return # the connection is already closed
client.stop()
self._model_api = None
self._connected = False
Expand Down

0 comments on commit d5c3ff0

Please sign in to comment.