在AWS SageMaker中停止运行时没有错误/警告输出的情况下,可以使用以下代码示例进行解决:
import boto3
import time
def stop_sagemaker_notebook(instance_name):
client = boto3.client('sagemaker')
# 获取SageMaker Notebook实例的描述
response = client.describe_notebook_instance(NotebookInstanceName=instance_name)
status = response['NotebookInstanceStatus']
# 如果实例正在运行,则停止实例
if status == 'InService':
client.stop_notebook_instance(NotebookInstanceName=instance_name)
# 等待实例停止
while status != 'Stopped':
response = client.describe_notebook_instance(NotebookInstanceName=instance_name)
status = response['NotebookInstanceStatus']
time.sleep(5)
print('SageMaker Notebook实例已停止')
else:
print('SageMaker Notebook实例已是停止状态')
# 指定SageMaker Notebook实例的名称
instance_name = 'your-instance-name'
# 停止SageMaker Notebook实例
stop_sagemaker_notebook(instance_name)
请确保您已正确配置AWS CLI,并且具有对SageMaker实例的适当权限。在代码中,将your-instance-name
替换为您要停止的SageMaker Notebook实例的名称。代码将检查实例的状态,如果实例正在运行,则停止实例,并等待实例停止。最后,将打印出"SageMaker Notebook实例已停止"或"SageMaker Notebook实例已是停止状态"的消息。