You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 22:13:09