AzureML Python SDK v2:如何在创建时以编程方式停止计算实例?

huangapple go评论51阅读模式
英文:

AzureML Python SDK v2: How to programmatically stop a compute instance upon creation?

问题

我正在开发一个脚本,如果计算实例尚不存在,就会创建计算实例。创建计算实例后,它处于运行状态。我想能够立即停止该计算实例,因为我暂时不需要它。

我想使用 Python 和“新”SDK v2 来实现这个目标。我对涉及空闲关闭时间和定期启动/停止的解决方案不感兴趣。我希望能直接控制实例的运行状态。

这段代码效果很好,但我真的想在创建后立即停止实例。在文档中我没有找到相关信息。

# 这段代码使用 SDK v2
# MC 是 MLClient... compute_instance 是 ComputeInstance 对象
mc.compute.begin_create_or_update(compute_instance)
compute_instance.wait()
compute_instance = compute_instance.result()

logger.info(f"已创建计算实例:名称={compute_instance.name} 大小={compute_instance.size}。")

if compute_instance.state == 'Running':
    # 我想在这里停止计算实例!
    logger.warning(f"计算实例 {compute_instance.name} 正在运行。这会消耗费用。")
英文:

I'm working on a script that creates compute instances if they don't already exist. Upon creating a compute instance, it is in running state. I'd like to be able to immediately stop that compute instance, as I won't be needing it immediately.

I want to do this using Python with the "new" SDK v2. I am not interested in solutions involving the Idle shutdown time, and scheduled start/stop. I am looking to have direct control on the running state of the instance.

This code works well, but I'd really like to stop the instance as soon as it's created. I haven't found anything relating to this in the documentation.

<!-- language: lang-py -->

# 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&quot;Created Compute instance: name={compute_instance.name} size={compute_instance.size}.&quot;)

if compute_instance.state == &#39;Running&#39;:
    # I&#39;d like to stop the compute instance HERE!
    logger.warning(f&quot;Compute instance {compute_instance.name} is RUNNING. This costs money.&quot;)

Thank you!

答案1

得分: 0

以下是翻译好的部分:

首先,按照下面的方式获取已创建计算实例的计算实例对象。

我的计算实例名称是 jgs-compute

from azureml.core import Workspace
from azureml.core.compute import ComputeInstance

ws = Workspace(workspace_name="jgsml", resource_group="your resource group", subscription_id="your subcription id")
compute_instance = ComputeInstance(ws, "jgs-compute")
print(f"计算实例:名称={compute_instance.name} 大小={compute_instance.vm_size}。")

然后执行以下操作以停止计算实例。

if compute_instance.status.state == 'Running':
    compute_instance.stop(wait_for_completion=True, show_output=True)
英文:

First get the compute instance object of created compute instance as below.

My compute instance name is jgs-compute

from  azureml.core  import  Workspace
from  azureml.core.compute  import  ComputeInstance

ws  =  Workspace(workspace_name=&quot;jgsml&quot;,resource_group=&quot;your resource group&quot;,subscription_id=&quot;your subcription id&quot;)
compute_instance  =  ComputeInstance(ws,&quot;jgs-compute&quot;)
print(f&quot;Compute instance: name={compute_instance.name} size={compute_instance.vm_size}.&quot;)

AzureML Python SDK v2:如何在创建时以编程方式停止计算实例?

Then execute below to stop.

if  compute_instance.status.state  ==  &#39;Running&#39;:
    compute_instance.stop(wait_for_completion=True,show_output=True)

AzureML Python SDK v2:如何在创建时以编程方式停止计算实例?

答案2

得分: 0

终于,我最终自己找到了答案,这种方法使用了 SDK 2.0。请注意,它是异步的,因此在停止完成之前,函数调用会返回。

MC 是 MLClient... compute_instance 是一个 ComputeInstance 对象

mc.compute.begin_stop(compute_instance.name)

英文:

Finally, I ended up finding my answer on my own, This method uses the SDK 2.0. Note that it is asynchronous, so the function call returns before the stopping is done.

# MC is MLClient... compute_instance is a ComputeInstance object    
mc.compute.begin_stop(compute_instance.name)

huangapple
  • 本文由 发表于 2023年5月26日 00:33:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定