首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python从Google中的操作名称轮询长时间操作的方式?

Python从Google中的操作名称轮询长时间操作的方式?
EN

Stack Overflow用户
提问于 2018-11-15 10:43:33
回答 3查看 1.2K关注 0票数 0

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

代码语言:javascript
复制
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)的内容:

代码语言:javascript
复制
rpc GetOperation(GetOperationRequest) returns (Operation)

其中,GetOperationRequest只需要操作名。是否有一种使用google-cloud-python库中的函数“重新创建”操作的方法?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-11-15 17:37:51

您可以使用get_operation方法的“长期运行的操作客户端”

代码语言:javascript
复制
from google.api_core import operations_v1
api = operations_v1.OperationsClient()
name = ...
response = api.get_operation(name)
票数 0
EN

Stack Overflow用户

发布于 2020-04-21 16:43:24

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

为了更新现有的操作,您需要将通道传递到OperationClient。

例如,备份一个Firestore数据存储。

代码语言:javascript
复制
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')
票数 1
EN

Stack Overflow用户

发布于 2021-08-18 15:04:35

在我的示例中,AutoML表客户端没有SERVICE_ADDRESSSCOPE属性,因此不能创建新的gRPC通道。

但是在客户端使用现有的一个似乎是有效的!

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53317626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档