从Google Cloud中的操作名称轮询长时间运行的操作的Python方法?

时间:2018-11-15 10:43:34

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

我正在调用Google Cloud Function,该函数返回实现Operation接口的google.longrunning.Operations对象。我想从另一个仅接收操作名称(将无权访问操作对象本身)的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库中的函数“重新创建”操作?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

为更多新近客户更新。您需要使用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')