【问题标题】:Python way of polling longrunning operations from operation name in Google Cloud?从谷歌云中的操作名称轮询长时间运行操作的 Python 方式?
【发布时间】:2019-04-18 10:45:00
【问题描述】:

我正在调用一个 Google Cloud 函数,该函数返回一个实现 google.longrunning.Operations 接口的 Operation 对象。我想从另一个仅接收操作名称的 Python 进程轮询此操作(无法访问操作对象本身)。所以我需要类似的东西:

operation = getOperation(operationName)
isdone = operation.done()

AFAIK,您无法执行上述第一步。我这里没找到:https://google-cloud-python.readthedocs.io/en/stable/core/operation.html

我想做有关google.longrunning 接口 (https://cloud.google.com/speech-to-text/docs/reference/rpc/google.longrunning#google.longrunning.Operations.GetOperation) 的文档中的说明:

rpc GetOperation(GetOperationRequest) returns (Operation)

GetOperationRequest 只需要操作名称。有没有办法使用google-cloud-python 库中的函数“重新创建”操作?

【问题讨论】:

    标签: python google-api google-cloud-platform google-cloud-functions


    【解决方案1】:

    就我而言,AutoML Tables Client 没有 SERVICE_ADDRESSSCOPE 属性,因此我无法创建新的 gRPC 通道。

    但在客户端使用现有的似乎可行!

    from google.api_core import operations_v1
    from google.cloud.automl_v1beta1 import TablesClient
    
    automl_tables_client = TablesClient(
        credentials=...,
        project=...,
        region=...,
    )
    
    operation_name = ""
    
    grpc_channel = automl_tables_client.auto_ml_client.transport._channel
    api_client = operations_v1.OperationsClient(grpc_channel)
    response = api_client.get_operation(operation_name)
    

    【讨论】:

      【解决方案2】:

      更新最近的客户。您需要使用 OperationClient 刷新操作:

      要更新现有操作,您需要将通道传递给 OperationClient。

      例如,备份 Firestore 数据存储。

      from google.cloud import firestore_admin_v1
      from google.api_core import operations_v1, grpc_helpers
      import time
      
      def main():
      
          client = firestore_admin_v1.FirestoreAdminClient()
          channel = grpc_helpers.create_channel(client.SERVICE_ADDRESS)
          api = operations_v1.OperationsClient(channel)
      
          db_path = client.database_path('myproject', 'mydb')
          operation = client.export_documents(db_path)
      
          current_status = api.get_operation(operation.name)
      
          while current_status.done == False:
      
             time.sleep(5)
             current_status = api.get_operation(operation.name)
             print('waiting to complete')
      
          print('operation done')
      
      

      【讨论】:

        【解决方案3】:

        可以使用"Long-Running Operations Client"get_operation方法:

        from google.api_core import operations_v1
        api = operations_v1.OperationsClient()
        name = ...
        response = api.get_operation(name)
        

        【讨论】:

        • 非常感谢!我正在使用ImageAnnotatorClient,调用async_batch_annotate_files。为了实例化OperationsClient(),我需要使用什么 gRPC 通道?似乎不推荐将 gRPC 通道传递给 ImageAnnotatorClient (googleapis.github.io/google-cloud-python/latest/vision/gapic/…)。再次感谢!
        • 如果你的客户是ia_client = ImageAnnotatorClient(...),你可以试试OperationsClient(ia_client.transport)吗?
        猜你喜欢
        • 1970-01-01
        • 2022-06-17
        • 1970-01-01
        • 2011-01-10
        • 2011-11-30
        • 2018-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多