![](/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.