我有多个为培训课程创建的 Sagemaker 笔记本。在新课程开始之前,我需要对笔记本进行更改。到目前为止,我认为执行此操作的唯一方法是使用更改更新存储库,然后单独进入每个笔记本实例并执行 git pull 来更新更改。有没有办法同时对与该存储库关联的所有笔记本执行此操作?
我想不出一种方法可以让 git 将更改单独拉入每个实例,但为了使其更容易(无论是现在还是将来),您可以考虑使用 Lifecycle Configuration。
程序看起来像这样:
import boto3
import time
lifecycle_config_name='YOUR_CONFIG_NAME'
sagemaker = boto3.client('sagemaker', region_name='us-west-2')
notebook_names = list(map(lambda x: x['NotebookInstanceName'], sagemaker.list_notebook_instances()['NotebookInstances']))
def stop_notebook(notebook_name):
print(f'Stopping {notebook_name}')
sagemaker.stop_notebook_instance(NotebookInstanceName=notebook_name)
while True:
if sagemaker.describe_notebook_instance(NotebookInstanceName=notebook_name)['NotebookInstanceStatus'] == 'Stopped':
break
else:
time.sleep(5)
def update_notebook(notebook_name):
print(f'Updating {notebook_name}')
sagemaker.update_notebook_instance(NotebookInstanceName=notebook_name, LifecycleConfigName=lifecycle_config_name)
while True:
if sagemaker.describe_notebook_instance(NotebookInstanceName=notebook_name)['NotebookInstanceStatus'] != 'Updating':
break
else:
time.sleep(5)
for notebook_name in notebook_names:
if sagemaker.describe_notebook_instance(NotebookInstanceName=notebook_name)['NotebookInstanceStatus'] == 'Pending':
stop_notebook(notebook_name)
update_notebook(notebook_name)
sagemaker.start_notebook_instance(NotebookInstanceName=notebook_name)