Espera las operaciones extendidas
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
En este ejemplo, se muestra cómo esperar a que se complete una operación extendida (de larga duración).
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","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)."]]