google API 中的 GetOperation
GetOperation in google API
我正在使用 dialogflow
python 包与我的 dialogflow 代理(v2
版本)通信。我调用 train_agent
函数并获取 google.api_core.operation.Operation
对象。我将 operation
的 name
属性存储在数据库中供以后使用。如何获得相同 operation
的新状态?
我尝试使用 from google.longrunning import GetOperation
,但它显示 ImportError: cannot import name GetOperation
。文档说 Any API service that returns long-running operations should implement the Operations interface so developers can have a consistent client experience.
我应该实现接口还是什么?
您似乎混淆了 Google 云 Python 客户端库与底层 Protobufs and gRPCs of the individual services. GetOperation
is a rpc method of the Operation
service, defined here。它需要一个 GetOperationRequest
消息作为输入,returns 一个 Operation
消息。 Protobuf 消息旨在以各种编程语言编译为本机 类,在 Python 中,它们实际上最终成为描述符。与长 运行 操作相关的所有消息的描述符都随 google-api-core
库一起提供,而每个 Google 云 Python 客户端库(例如 dialogflow
).这就是为什么您可以通过从客户端库导入它们来在技术上直接使用 protobuf 描述符的原因:
from google.longrunning.operations_pb2 import Operation
然后您只需生成 gRPC 客户端和服务器存根,就可以开始了:)
在现实生活中你当然应该忽略所有这些(包括 GetOperation
方法和 Operations
接口),而是使用 Google Cloud client libraries,其中有一个用于每个 Google 每种主要编程语言的云服务,并且都带有用于常见任务的核心模块,例如身份验证、计费和长期 运行 运营管理。这些库为最终用户抽象了 Protobuf/gRPC 内容。
在您的情况下,您需要查看 google.api_core.operations_v1.OperationsClient
, which you can use to manage instances of google.api_core.operation.Operation
.
另一个问题是您是否真的需要它。我从未见过训练 Dialogflow 代理需要超过几秒钟的时间,而且一旦设置了代理,它也不是必须一遍又一遍地完成的事情。您真的必须手动管理此操作,甚至将有关它的元数据存储在外部数据库中吗?
所以我最终使用 AgentsClient()
对象来访问操作客户端。
最终代码:
agent_client = dialogflow_v2.AgentsClient()
operations_obj = agent_client.operations_client.get_operations(operations_full_path)
status = operations_obj.done
和文档中提到的其他字段可以使用。
我正在使用 dialogflow
python 包与我的 dialogflow 代理(v2
版本)通信。我调用 train_agent
函数并获取 google.api_core.operation.Operation
对象。我将 operation
的 name
属性存储在数据库中供以后使用。如何获得相同 operation
的新状态?
我尝试使用 from google.longrunning import GetOperation
,但它显示 ImportError: cannot import name GetOperation
。文档说 Any API service that returns long-running operations should implement the Operations interface so developers can have a consistent client experience.
我应该实现接口还是什么?
您似乎混淆了 Google 云 Python 客户端库与底层 Protobufs and gRPCs of the individual services. GetOperation
is a rpc method of the Operation
service, defined here。它需要一个 GetOperationRequest
消息作为输入,returns 一个 Operation
消息。 Protobuf 消息旨在以各种编程语言编译为本机 类,在 Python 中,它们实际上最终成为描述符。与长 运行 操作相关的所有消息的描述符都随 google-api-core
库一起提供,而每个 Google 云 Python 客户端库(例如 dialogflow
).这就是为什么您可以通过从客户端库导入它们来在技术上直接使用 protobuf 描述符的原因:
from google.longrunning.operations_pb2 import Operation
然后您只需生成 gRPC 客户端和服务器存根,就可以开始了:)
在现实生活中你当然应该忽略所有这些(包括 GetOperation
方法和 Operations
接口),而是使用 Google Cloud client libraries,其中有一个用于每个 Google 每种主要编程语言的云服务,并且都带有用于常见任务的核心模块,例如身份验证、计费和长期 运行 运营管理。这些库为最终用户抽象了 Protobuf/gRPC 内容。
在您的情况下,您需要查看 google.api_core.operations_v1.OperationsClient
, which you can use to manage instances of google.api_core.operation.Operation
.
另一个问题是您是否真的需要它。我从未见过训练 Dialogflow 代理需要超过几秒钟的时间,而且一旦设置了代理,它也不是必须一遍又一遍地完成的事情。您真的必须手动管理此操作,甚至将有关它的元数据存储在外部数据库中吗?
所以我最终使用 AgentsClient()
对象来访问操作客户端。
最终代码:
agent_client = dialogflow_v2.AgentsClient()
operations_obj = agent_client.operations_client.get_operations(operations_full_path)
status = operations_obj.done
和文档中提到的其他字段可以使用。