![](/img/trans.png)
[英]Long running python process with Google Cloud Functions
[英]How best to handle async response from google long running operation with cloud functions
我正在使用 Google Cloud Functions (python) 通过在此处调用 exportAssets() 方法从 GCP 启动资产清单导出。 该方法返回此处定义的操作 object,可用于轮询操作,直到操作完成。 当然,因为这是一个云 function 我被限制在 540 秒,所以不能永远这样做。 google api python 客户端提供了 add_done_callback() 方法,人们可以在其中等待异步响应,但据我所知,它要求我在云中保持线程处于活动状态 ZC1C425268E68385D1AB5074F 有没有办法告诉资产清单 API 执行操作以将 aync 响应(成功或失败)发送到我可以正确处理响应的 pubsub 主题? 试图避免使用 basic_scaling 启动 appengine 实例以支持 24 小时超时。
from google.cloud import asset_v1
# .....
# Setup request to asset inventory API
parent = "organizations/{}".format(GCP_ORGANIZATION)
requested_type = 'RESOURCE'
dataset = 'projects/{}/datasets/gcp_assets_{}'.format(GCP_PROJECT, requested_type)
partition_spec = asset_v1.PartitionSpec
partition_key = asset_v1.PartitionSpec.PartitionKey.REQUEST_TIME
partition_spec.partition_key = asset_v1.PartitionSpec.PartitionKey.REQUEST_TIME
output_config = asset_v1.OutputConfig()
output_config.bigquery_destination.dataset = dataset
output_config.bigquery_destination.table = 'assets'
output_config.bigquery_destination.separate_tables_per_asset_type = True
output_config.bigquery_destination.partition_spec.partition_key = partition_key
# Make API request to asset inventory API
print("Creating job to load 'asset types: {}' to {}".format(
requested_type,
dataset
))
response = ASSET_CLIENT.export_assets(
request={
"parent": parent,
"content_type": content_type,
"output_config": output_config,
}
)
print(response.result()) # This waits for the job to complete
Cloud Asset 清单导出不会在导出结束时提供 PubSub 通知。 但是,在我之前的公司中,导出 100k+ 资产大约需要 5 分钟; 这还不错,如果你有更多的资产。 我确定您可以联系 Google Cloud(使用您的客户工程师)在路线图中添加此通知。
无论如何,如果你想构建一个解决方法,你可以使用workflows 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.