簡體   English   中英

如何使用 Boto3 在 AWS EMR 集群中等待步驟完成

[英]How to wait for a step completion in AWS EMR cluster using Boto3

給定一個步驟 ID,我想等待該 AWS EMR 步驟完成。 我怎樣才能做到這一點? 有內置函數嗎?

在撰寫本文時,用於 EMR 的 Boto3 Waiters 允許等待集群運行和集群終止事件:

EMR服務員

現在有一個可用於步驟完成事件的服務員。 它是在最近的 boto3 版本中添加的。

http://boto3.readthedocs.io/en/latest/reference/services/emr.html#EMR.Waiter.StepComplete

示例代碼:

import boto3

client = boto3.client("emr")
waiter = client.get_waiter("step_complete")
waiter.wait(
    ClusterId='the-cluster-id',
    StepId='the-step-id',
    WaiterConfig={
        "Delay": 30,
        "MaxAttempts": 10
    }
)

Boto3 中沒有內置函數。 但是您可以編寫自己的服務員。

請參閱: describe_step

使用cluster_idstep_id調用describe_step 響應是一個字典,其中包含有關該步驟的詳細信息。 鍵之一是“狀態”,其中包含有關步驟狀態的信息。 如果狀態未完成,請等待幾秒鍾再試一次,直到狀態完成或等待時間超過您的限制。

'State': 'PENDING'|'CANCEL_PENDING'|'RUNNING'|'COMPLETED'|'CANCELLED'|'FAILED'|'INTERRUPTED'

我想出了以下代碼(如果您將max_attempts設置為 0 或更少,那么它只會等待直到沒有正在運行/待處理的步驟):

def wait_for_steps_completion(emr_client, emr_cluster_id, max_attempts=0):
    sleep_seconds = 30
    num_attempts = 0

    while True:
        response = emr_client.list_steps(
            ClusterId=emr_cluster_id,
            StepStates=['PENDING', 'CANCEL_PENDING', 'RUNNING']
        )
        num_attempts += 1
        active_aws_emr_steps = response['Steps']

        if active_aws_emr_steps:
            if 0 < max_attempts <= num_attempts:
                raise Exception(
                    'Max attempts exceeded while waiting for AWS EMR steps completion. Last response:\n'
                    + json.dumps(response, indent=3, default=str)
                )
            time.sleep(sleep_seconds)
        else:
            return

我在GitHub 上編寫了一個通用的 status_poller 函數,作為 EMR 交互式演示的一部分。

status_poller 函數循環並調用一個函數,打印“.”。 或新狀態,直到返回指定狀態:

def status_poller(intro, done_status, func):
    """
    Polls a function for status, sleeping for 10 seconds between each query,
    until the specified status is returned.
    :param intro: An introductory sentence that informs the reader what we're
                  waiting for.
    :param done_status: The status we're waiting for. This function polls the status
                        function until it returns the specified status.
    :param func: The function to poll for status. This function must eventually
                 return the expected done_status or polling will continue indefinitely.
    """
    status = None
    print(intro)
    print("Current status: ", end='')
    while status != done_status:
        prev_status = status
        status = func()
        if prev_status == status:
            print('.', end='')
        else:
            print(status, end='')
        sys.stdout.flush()
        time.sleep(10)
    print()

要檢查步驟完成,您可以這樣稱呼它:

status_poller(
    "Waiting for step to complete...",
    'COMPLETED',
    lambda:
    emr_basics.describe_step(cluster_id, step_id, emr_client)['Status']['State'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM