Attendi le operazioni estese
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questo esempio mostra come attendere il completamento di un'operazione (a lunga esecuzione) estesa.
Esempio di codice
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis sample demonstrates how to wait for a long-running operation to complete using the \u003ccode\u003ewait_for_extended_operation\u003c/code\u003e function.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code includes a function, \u003ccode\u003ewait_for_extended_operation\u003c/code\u003e, that handles waiting for an operation, reporting errors or warnings, and returning the operation's result.\u003c/p\u003e\n"],["\u003cp\u003eThe function accepts an \u003ccode\u003eExtendedOperation\u003c/code\u003e object, a verbose name for the operation, and a timeout duration as arguments.\u003c/p\u003e\n"],["\u003cp\u003eThe sample requires setting up Application Default Credentials for authentication and also references Compute Engine quickstart and API documentation for further setup and information.\u003c/p\u003e\n"],["\u003cp\u003eThe documentation also references a sample browser to search for other Google Cloud product code samples.\u003c/p\u003e\n"]]],[],null,["# Wait for extended operations\n\nThis sample demonstrates how to wait for an extended (long-running) operation to complete.\n\nCode sample\n-----------\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Compute Engine quickstart using\nclient libraries](/compute/docs/api/using-libraries).\n\n\nFor more information, see the\n[Compute Engine Python API\nreference documentation](/python/docs/reference/compute/latest).\n\n\nTo authenticate to Compute Engine, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from __future__ import annotations\n\n import sys\n from typing import Any\n\n from google.api_core.extended_operation import ExtendedOperation\n\n\n def wait_for_extended_operation(\n operation: ExtendedOperation, verbose_name: str = \"operation\", timeout: int = 300\n ) -\u003e Any:\n \"\"\"\n Waits for the extended (long-running) operation to complete.\n\n If the operation is successful, it will return its result.\n If the operation ends with an error, an exception will be raised.\n If there were any warnings during the execution of the operation\n they will be printed to sys.stderr.\n\n Args:\n operation: a long-running operation you want to wait on.\n verbose_name: (optional) a more verbose name of the operation,\n used only during error and warning reporting.\n timeout: how long (in seconds) to wait for operation to finish.\n If None, wait indefinitely.\n\n Returns:\n Whatever the operation.result() returns.\n\n Raises:\n This method will raise the exception received from `operation.exception()`\n or RuntimeError if there is no exception set, but there is an `error_code`\n set for the `operation`.\n\n In case of an operation taking longer than `timeout` seconds to complete,\n a `concurrent.futures.TimeoutError` will be raised.\n \"\"\"\n result = operation.result(timeout=timeout)\n\n if operation.error_code:\n print(\n f\"Error during {verbose_name}: [Code: {operation.error_code}]: {operation.error_message}\",\n file=sys.stderr,\n flush=True,\n )\n print(f\"Operation ID: {operation.name}\", file=sys.stderr, flush=True)\n raise operation.exception() or RuntimeError(operation.error_message)\n\n if operation.warnings:\n print(f\"Warnings during {verbose_name}:\\n\", file=sys.stderr, flush=True)\n for warning in operation.warnings:\n print(f\" - {warning.code}: {warning.message}\", file=sys.stderr, flush=True)\n\n return result\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=compute)."]]