Sostituire la funzione sperimentale con il passaggio parallelo

La funzione sperimentale dei workflow , experimental.executions.map, avvia l'esecuzione di un workflow per ogni argomento corrispondente e attende il completamento di tutte le esecuzioni, restituendo un elenco in cui ogni elemento è il risultato di un'esecuzione.

Se utilizzi experimental.executions.map per supportare il lavoro parallelo, puoi eseguire la migrazione del workflow per utilizzare invece i passaggi paralleli, eseguendo i normali for loop in parallelo.

Un passaggio parallel definisce una parte del workflow in cui due o più passaggi possono essere eseguiti contemporaneamente. Un passaggio parallel attende il completamento o l'interruzione di tutti i passaggi definiti al suo interno a causa di un'eccezione non gestita; l'esecuzione continua. Come experimental.executions.map, l'ordine di esecuzione non è garantito. Per maggiori dettagli, consulta la pagina di riferimento per la sintassi dei passaggi paralleli.

Tieni presente che l'utilizzo di experimental.executions.map o workflows.executions.run richiede una quota di esecuzioni simultanee aggiuntiva. Tuttavia, quando utilizzi passaggi paralleli con chiamate ai connettori in linea (vedi l'esempio del connettore di traduzione), non è richiesta alcuna quota di esecuzione aggiuntiva.

Gli esempi seguenti hanno lo scopo di aiutarti a sostituire l'utilizzo di experimental.executions.map con un passaggio parallel.

Workflow di traduzione

Dati una lingua di origine e una di destinazione, il seguente workflow, denominato translate, utilizza il connettore Cloud Translation per tradurre un testo di input e restituire il risultato. Tieni presente che l'API Cloud Translation deve essere abilitata.

YAML

  main:
    params: [args]
    steps:
    - basic_translate:
        call: googleapis.translate.v2.translations.translate
        args:
          body:
            q: ${args.text}
            target: ${args.target}
            format: "text"
            source: ${args.source}
        result: r
    - return_step:
        return: ${r}

JSON

  {
    "main": {
      "params": [
        "args"
      ],
      "steps": [
        {
          "basic_translate": {
            "call": "googleapis.translate.v2.translations.translate",
            "args": {
              "body": {
                "q": "${args.text}",
                "target": "${args.target}",
                "format": "text",
                "source": "${args.source}"
              }
            },
            "result": "r"
          }
        },
        {
          "return_step": {
            "return": "${r}"
          }
        }
      ]
    }
  }

Input per l'esempio precedente:

{
  "text": "Bonjour",
  "target": "en",
  "source": "fr"
}

L'output dovrebbe essere simile al seguente:

{
 "data": {
   "translations": [
     {
       "translatedText": "Hello"
     }
   ]
 }
}

Workflow di traduzione batch che utilizza experimental.executions.map

Il seguente workflow traduce un batch di testi. Per ogni input, experimental.executions.map esegue il translate workflow creato in precedenza.

YAML

  main:
     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"
     - translate_texts:
         call: experimental.executions.map
         args:
             workflow_id: ${workflow_id}
             arguments: ${texts_to_translate}
         result: translated
     - return:
             return: ${translated}

JSON

  {
    "main": {
      "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"
                  }
                ]
              }
            ]
          }
        },
        {
          "translate_texts": {
            "call": "experimental.executions.map",
            "args": {
              "workflow_id": "${workflow_id}",
              "arguments": "${texts_to_translate}"
            },
            "result": "translated"
          }
        },
        {
          "return": {
            "return": "${translated}"
          }
        }
      ]
    }
  }

L'output dovrebbe essere simile al seguente:

[
  {
    "data": {
      "translations": [
        {
          "translatedText": "Bonjour le monde!"
        }
      ]
    }
  },
  {
    "data": {
      "translations": [
        {
          "translatedText": "Hello world!"
        }
      ]
    }
  },
  {
    "data": {
      "translations": [
        {
          "translatedText": "I don't speak Spanish!"
        }
      ]
    }
  }
]

Sostituisci experimental.executions.map con un loop for:in

Anziché utilizzare la funzione sperimentale, puoi utilizzare un loop for:in parallelo per tradurre il testo. Nell'esempio seguente, il workflow secondario, translate, può essere utilizzato così com'è e l'output dovrebbe rimanere invariato. Hai anche la possibilità di avviare altre esecuzioni di workflow secondari in rami paralleli.

Una variabile condivisa, translated, viene utilizzata per archiviare i risultati e viene compilata con stringhe vuote per consentire l'indicizzazione statica degli array. Se l'ordinamento non è obbligatorio, puoi utilizzare list.concat per aggiungere i risultati. Tutte le assegnazioni nei passaggi paralleli sono atomiche.

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:
            in: ${texts_to_translate}
            index: i  # optional, use if index is required
            value: arg
            steps:
              - translate:
                  call: googleapis.workflowexecutions.v1.projects.locations.workflows.executions.run
                  args:
                    workflow_id: ${workflow_id}
                    argument: ${arg}
                  result: r
              - set_result:
                  assign:
                    - translated[i]: ${r}
    - return:
        return: ${translated}

JSON

{
  "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": [
                "",
                "",
                ""
              ]
            }
          ]
        }
      },
      {
        "parallel_translate": {
          "parallel": {
            "shared": [
              "translated"
            ],
            "for": {
              "in": "${texts_to_translate}",
              "index": "i",
              "value": "arg",
              "steps": [
                {
                  "translate": {
                    "call": "googleapis.workflowexecutions.v1.projects.locations.workflows.executions.run",
                    "args": {
                      "workflow_id": "${workflow_id}",
                      "argument": "${arg}"
                    },
                    "result": "r"
                  }
                },
                {
                  "set_result": {
                    "assign": [
                      {
                        "translated[i]": "${r}"
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      },
      {
        "return": {
          "return": "${translated}"
        }
      }
    ]
  }
}

Sostituisci experimental.executions.map con un loop for:range

Anziché utilizzare la funzione sperimentale, puoi utilizzare un loop for:range parallelo per tradurre il testo. Utilizzando un loop for:range, puoi specificare l'inizio e la fine di un intervallo di iterazione. L'output dovrebbe rimanere invariato.

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}

JSON

{
  "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": [
                "",
                "",
                ""
              ]
            }
          ]
        }
      },
      {
        "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}"
        }
      }
    ]
  }
}

Incorpora il codice sorgente del workflow

Se il workflow secondario è relativamente breve, potresti volerlo includere direttamente nel workflow principale per una maggiore leggibilità. Ad esempio, nel seguente workflow, il codice sorgente del workflow translate è stato incorporato.

YAML

main:
  params: [args]
  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: ["", "", ""]
    - parallel_translate:
        parallel:
          shared: [translated]  # to write to this variable, you must share it
          for:
            range: ${[0, len(texts_to_translate) - 1]}
            value: i
            steps:
              - basic_translate:
                  call: googleapis.translate.v2.translations.translate
                  args:
                    body:
                      q: ${args.text}
                      target: ${args.target}
                      format: "text"
                      source: ${args.source}
                  result: r
              - set_result:
                  assign:
                    - translated[i]: ${r}
    - return:
        return: ${translated}

JSON

{
  "main": {
    "params": [
      "args"
    ],
    "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": [
                "",
                "",
                ""
              ]
            }
          ]
        }
      },
      {
        "parallel_translate": {
          "parallel": {
            "shared": [
              "translated"
            ],
            "for": {
              "range": "${[0, len(texts_to_translate) - 1]}",
              "value": "i",
              "steps": [
                {
                  "basic_translate": {
                    "call": "googleapis.translate.v2.translations.translate",
                    "args": {
                      "body": {
                        "q": "${args.text}",
                        "target": "${args.target}",
                        "format": "text",
                        "source": "${args.source}"
                      }
                    },
                    "result": "r"
                  }
                },
                {
                  "set_result": {
                    "assign": [
                      {
                        "translated[i]": "${r}"
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      },
      {
        "return": {
          "return": "${translated}"
        }
      }
    ]
  }
}

Passaggi successivi