上傳結構化資料檔案後,資源不會立即建立或更新。 Display & Video 360 處理結構化資料檔案並進行更新,可能需要幾秒到幾小時不等。只要檔案不含任何重疊資源,即可同時上傳多個檔案。
如要判斷工作是否完成,請定期使用 sdfuploadtasks.operations.get 擷取作業,並檢查 done 欄位是否為 True,以及工作是否完成。這個程序稱為「輪詢」。
如果工作已完成,系統會設定 response 或 error 欄位。如果 error 欄位已填入資料,表示工作失敗,且結果 Status 物件中會提供失敗詳細資料。如果填入 response 欄位,系統會為該欄位指派 SdfUploadTask。這表示 Display & Video 360 已成功處理上傳的結構化資料檔案。從 SdfUploadTask.resourceName 欄位提供的路徑下載結果檔案。
如果輪詢實作項目效率不彰,檢查長時間執行的報表時,會耗用大量 API 請求配額。如要限制重試次數並節省配額,請使用指數輪詢。
以下說明如何使用指數輪詢輪詢結構化資料檔案上傳作業:
# Import the necessary libraries import random import time # Provide the name of the sdfuploadtask operation. operation_name = operation-name # Set the following values that control retry behavior while the operation # is running. # Minimum amount of time between polling requests. Defaults to 5 seconds. min_retry_interval = 5 # Maximum amount of time between polling requests. Defaults to 5 minutes. max_retry_interval = 5 * 60 # Maximum amount of time to spend polling. Defaults to 5 hours. max_retry_elapsed_time = 5 * 60 * 60 # Configure the sdfuploadtasks.operations.get request. get_request = service.sdfuploadtasks().operations().get(operation_name) sleep = 0 start_time = time.time() while True: # Get current status of the report. operation = get_request.execute() if "done" in operation: if "error" in operation: print( f'The operation finished in error with code ' f'{operation["error"]["code"]}: {operation["error"]["message"]}') else: print( f'The operation completed successfully. The result files can be ' f'downloaded at {operation["response"]["resourceName"]}.') break elif time.time() - start_time > max_retry_elapsed_time: print("Polling deadline exceeded.") break sleep = next_sleep_interval(sleep) print( f'Operation {operation_name} is still running, sleeping for ' f'{sleep} seconds.') time.sleep(sleep) def next_sleep_interval(previous_sleep_interval): """Calculates the next sleep interval based on the previous.""" min_interval = previous_sleep_interval or min_retry_interval max_interval = previous_sleep_interval * 3 or min_retry_interval return min(max_retry_interval, random.randint(min_interval, max_interval))