Skip to content Skip to sidebar Skip to footer

Python Way Of Polling Longrunning Operations From Operation Name In Google Cloud?

I'm calling a Google Cloud Function that returns an Operation object implementing the google.longrunning.Operations interface. I want to poll this operation from another Python pro

Solution 1:

Update for more recent clients. You need to refresh the operation using the OperationClient:

For updating an existing operation you will need to pass the channel across to the OperationClient.

For example, backing up a Firestore datastore.

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')

Solution 2:

In my case, The AutoML Tables Client didn't have a SERVICE_ADDRESS or SCOPE properties, so I can't create a new gRPC channel.

But using the existing one in the client seems to work!

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)

Solution 3:

You can use the get_operation method of the "Long-Running Operations Client":

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

Post a Comment for "Python Way Of Polling Longrunning Operations From Operation Name In Google Cloud?"