Perform parallel iteration using for loops

Executes loop iterations in parallel to reduce the end-to-end execution time. A shared variable is updated in each iteration.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

YAML

main:
  params: [args]
  steps:
    - init:
        assign:
          - total: 0
    - countComments:
        parallel:
          shared: [total]
          for:
            value: postId
            in: ${args.posts}
            steps:
              - getPostCommentCount:
                  call: http.get
                  args:
                    url: ${"https://example.com/postComments/" + postId}
                  result: numComments
              - add:
                  assign:
                    - total: ${total + numComments}
    - done:
        return: ${total}

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.