上传的结构化数据文件不会立即创建或更新资源。 Display & Video 360 可能需要几秒到几小时的时间来处理您的 SDF 并进行相应的更新。只要多个文件不包含任何重叠的资源,就可以同时上传。
如需确定任务是否已完成,请定期使用 sdfuploadtasks.operations.get
检索操作,并检查 done
字段是否为 True
,以及任务是否已完成。此过程称为“轮询”。
如果任务已完成,则会设置 response
或 error
字段。如果 error
字段已填充,则表示任务失败,并且结果 Status
对象中提供了有关失败的详细信息。如果 response
字段已填充,系统会将 SdfUploadTask
分配给该字段。这意味着 Display & Video 360 已成功处理上传的 SDF。从 SdfUploadTask.resourceName
字段中提供的位置下载结果文件。
如果轮询实现代码效率低下,在检查需要长时间才能生成的报告时,会消耗大量 API 请求配额。为了限制重试次数并节省配额,请使用指数退避算法。
以下是如何使用指数退避算法轮询 SDF 上传操作:
# 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))