Sostituire la funzione sperimentale con il passaggio parallelo

La funzione sperimentale Workflows, experimental.executions.map, avvia un'esecuzione del 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 migrare il flusso di lavoro per utilizzare passaggi paralleli, eseguendo normali loop for in parallelo.

Un passaggio parallel definisce una parte del flusso di lavoro in cui due o più passaggi possono essere eseguiti contemporaneamente. Un passaggio parallel attende il completamento di tutti i passaggi definiti al suo interno o l'interruzione dovuta a un'eccezione non gestita, dopodiché l'esecuzione continua. Come experimental.executions.map, l'ordine di esecuzione non è garantito. Per maggiori dettagli, consulta la pagina di riferimento della sintassi per i 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 di connettore di traduzione), non è richiesta alcuna quota di esecuzione aggiuntiva.

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

Flusso di lavoro di traduzione

Dato un linguaggio di origine e di destinazione, il seguente flusso di lavoro, 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"
     }
   ]
 }
}

Flusso di lavoro di traduzione batch utilizzando experimental.executions.map

Il seguente flusso di lavoro traduce un batch di testi. Per ogni input, experimental.executions.map esegue il flusso di lavoro translate 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 di for:in

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

Una variabile condivisa, translated, viene utilizzata per archiviare i risultati e viene completata 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 di for:range

Anziché utilizzare la funzione sperimentale, puoi utilizzare un ciclo for:range parallelo per tradurre il testo. Utilizzando un ciclo 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 includerlo direttamente nel workflow principale per una maggiore leggibilità. Ad esempio, nel seguente flusso di lavoro, il codice sorgente del flusso di lavoro 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