执行并行 for:range 循环

使用并行 for:range 循环来翻译文本,并指定迭代范围的开始和结束。

深入探索

如需查看包含此代码示例的详细文档,请参阅以下内容:

代码示例

YAML

main:
  params: []
  steps:
    - init:
        assign:
          - workflow_id: "translate"
          - texts_to_translate:
              - text: "hello world!"
                source: "en"
                target: "fr"
              - text: "你好 世界!"
                source: "zh-CN"
                target: "en"
              - text: "No hablo español!"
                source: "es"
                target: "en"
          - translated: ["", "", ""]  # to write to this variable, you must share it
    - parallel_translate:
        parallel:
          shared: [translated]
          for:
            range: ${[0, len(texts_to_translate) - 1]}
            value: i
            steps:
              - translate:
                  call: googleapis.workflowexecutions.v1.projects.locations.workflows.executions.run
                  args:
                    workflow_id: ${workflow_id}
                    argument: ${texts_to_translate[i]}
                  result: r
              - set_result:
                  assign:
                    - translated[i]: ${r}
    - return:
        return: ${translated}

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器