Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add finalizer to Connection #242

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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