轮询长时间运行的操作
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如要轮询操作,请重复调用 get_long_running_operation()
方法,直到操作完成。在每个轮询请求之间使用退避时间,例如 10 秒。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[[["\u003cp\u003eTo check the status of a long-running operation, repeatedly call the \u003ccode\u003eget_long_running_operation()\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eIt is recommended to implement a backoff, such as a 10-second delay, between each polling request.\u003c/p\u003e\n"],["\u003cp\u003eThe provided Python code sample demonstrates how to poll an operation until it completes, using a \u003ccode\u003ewhile\u003c/code\u003e loop to continue checking.\u003c/p\u003e\n"],["\u003cp\u003eSetting up Application Default Credentials is required for authenticating to Document AI, and more information is provided in the link.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003epoll_operation_sample\u003c/code\u003e function in the provided code can be used to perform the polling of an operation.\u003c/p\u003e\n"]]],[],null,["# Poll a long-running operation\n\nTo poll an operation, repeatedly call the `get_long_running_operation()` method until the operation finishes. Use a backoff between each poll request, such as 10 seconds.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Managing long-running operations (LROs)](/document-ai/docs/long-running-operations)\n\nCode sample\n-----------\n\n### Python\n\n\nFor more information, see the\n[Document AI Python API\nreference documentation](/python/docs/reference/documentai/latest).\n\n\nTo authenticate to Document AI, 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\n from time import sleep\n\n from google.api_core.client_options import ClientOptions\n from google.cloud import documentai # type: ignore\n from google.longrunning.operations_pb2 import GetOperationRequest # type: ignore\n\n # TODO(developer): Uncomment these variables before running the sample.\n # location = \"YOUR_PROCESSOR_LOCATION\" # Format is \"us\" or \"eu\"\n # operation_name = \"YOUR_OPERATION_NAME\" # Format is \"projects/{project_id}/locations/{location}/operations/{operation_id}\"\n\n\n def poll_operation_sample(location: str, operation_name: str) -\u003e None:\n # You must set the `api_endpoint` if you use a location other than \"us\".\n opts = ClientOptions(api_endpoint=f\"{location}-documentai.googleapis.com\")\n client = documentai.https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.services.document_processor_service.DocumentProcessorServiceClient.html(client_options=opts)\n\n request = GetOperationRequest(name=operation_name)\n\n while True:\n # Make GetOperation request\n operation = client.https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.services.document_processor_service.DocumentProcessorServiceClient.html#google_cloud_documentai_v1_services_document_processor_service_DocumentProcessorServiceClient_get_operation(request=request)\n # Print the Operation Information\n print(operation)\n\n # Stop polling when Operation is no longer running\n if operation.done:\n break\n\n # Wait 10 seconds before polling again\n sleep(10)\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=documentai)."]]