Skip to content

Remove singleton metaclass dependency for SageMakerClient #312

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 17 additions & 1 deletion src/sagemaker_core/main/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,19 @@ def __call__(cls, *args, **kwargs):
return cls._instances[cls]


class SageMakerClient(metaclass=SingletonMeta):
class SageMakerClient:
"""
A singleton class for creating a SageMaker client.
"""

_instance = None
_initialized = False

def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance

def __init__(
self,
session: Session = None,
Expand All @@ -335,6 +343,12 @@ def __init__(
Initializes the SageMakerClient with a boto3 session, region name, and service name.
Creates a boto3 client using the provided session, region, and service.
"""

# Only skip reinitialization if region is the same
if self._initialized and session is None and config is None:
if region_name is None or region_name == self._instance.region_name:
return

if session is None:
logger.warning("No boto3 session provided. Creating a new session.")
session = Session(region_name=region_name)
Expand All @@ -361,6 +375,8 @@ def __init__(
"sagemaker-metrics", region_name, config=self.config
)

self._initialized = True

def get_client(self, service_name: str) -> Any:
"""
Get the client of corresponding service
Expand Down