【问题标题】:How to use Google Cloud get_operation?如何使用谷歌云 get_operation?
【发布时间】:2019-07-30 02:37:46
【问题描述】:

我正在尝试使用 OperationsClient get_operation() 函数,以便轮询长时间运行的任务并获取其状态。

https://google-cloud-python.readthedocs.io/en/stable/core/operations_client.html#google.api_core.operations_v1.OperationsClient.get_operation

代码看起来相当简单:

api = operations_v1.OperationsClient()
response = api.get_operation(name)

但是,OperationClient 的初始化需要一个通道:OperationsClient(channel)。我对 channel 的作用或它的创建方式一无所知。

任何帮助表示赞赏。谢谢

【问题讨论】:

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


    【解决方案1】:

    来自您链接的文档:

    channel (grpc.Channel) – 与实现 google.longrunning.operations 接口的服务关联的 gRPC 通道。

    因此,如果它是您自己的 gRPC 服务,您需要执行以下操作:

    import grpc
    channel = grpc.insecure_channel('localhost:50051')
    api = operations_v1.OperationsClient(channel)
    response = api.get_operation(name)
    

    根据您尝试轮询的长时间运行的服务,您可能需要改用其频道。

    【讨论】:

    • 如果不是我自己的 gRPC 服务怎么办?目前我只是使用 SpeechClient 在音频文件上运行 long_running_recognize。我有操作的名称,只需要弄清楚如何轮询状态。
    【解决方案2】:

    这是我最终使用的代码。

    operation = self.stt_client.transport._operations_client.get_operation(operation_name)
    response = operation_core.from_gapic(
                operation,
                self.stt_client.transport._operations_client,
                types.LongRunningRecognizeResponse,
                metadata_type=types.LongRunningRecognizeMetadata,
            )
    

    【讨论】:

    • 本例中的 self.stt_client 是什么?是 google.cloud.speech.SpeechClient() 吗?
    猜你喜欢
    • 2021-05-13
    • 2017-05-01
    • 2018-07-08
    • 2022-12-17
    • 2017-11-10
    • 2020-10-03
    • 2020-09-10
    • 2021-02-28
    • 2020-10-14
    相关资源
    最近更新 更多