如何使用AzureML Python SDK v2创建后立即停止计算实例?
解决方案
要在AzureML SDK v2中直接停止运行状态的Compute Instance,你可以使用MLClient.compute.begin_stop()方法,该方法专门用于触发实例的停止操作。
修改后的完整代码
# This code uses SDK v2 # MC is MLClient... compute_instance is a ComputeInstance object mc.compute.begin_create_or_update(compute_instance) compute_instance.wait() compute_instance = compute_instance.result() logger.info(f"Created Compute instance: name={compute_instance.name} size={compute_instance.size}.") if compute_instance.state == 'Running': # 触发停止操作 stop_operation = mc.compute.begin_stop(compute_instance.name) # 等待停止操作完成 stop_operation.wait() # 获取更新后的实例状态 updated_instance = stop_operation.result() logger.info(f"Compute instance {updated_instance.name} has been stopped. Current state: {updated_instance.state}") logger.warning(f"Compute instance {compute_instance.name} was stopped to avoid unnecessary costs.")
关键说明
begin_stop()方法接受Compute Instance的名称作为参数,返回一个LROPoller对象,用于跟踪异步操作的状态- 调用
wait()方法会阻塞当前线程,直到停止操作完成 - 通过
result()方法可以获取停止后的实例对象,确认状态已更新为Stopped
内容的提问来源于stack exchange,提问作者jpmartineau
相关产品推荐
相关产品推荐

