I file di dati strutturati non vengono generati istantaneamente. Display & Video 360 potrebbe impiegare da pochi secondi a ore per generare i tuoi SDF.
Per determinare se l'attività è terminata, recupera regolarmente l'operazione utilizzando
sdfdownloadtasks.operations.get e controlla se il campo
done è True e l'attività è terminata. Questa procedura è
nota come "polling".
Se l'attività è terminata, viene impostato il campo response o error. Se il campo error è compilato, l'attività non è riuscita e i dettagli sull'errore sono disponibili nell'oggetto Status risultante. Se il campo response è compilato, Display & Video 360 ha generato correttamente gli SDF. Scarica i file risultanti dalla posizione indicata in response.resourceName.
Un'implementazione di polling inefficiente che controlla un report a esecuzione prolungata consuma gran parte della quota di richieste API. Per limitare i tentativi e conservare la quota, utilizza il backoff esponenziale.
Ecco come eseguire il polling di un'operazione di download di SDF utilizzando il backoff esponenziale:
# Provide the name of the sdfdownloadtask 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 sdfdownloadtasks.operations.get request. get_request = service.sdfdownloadtasks().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 resulting files can be ' f'downloaded at {operation["response"]["resourceName"]}.') break elif time.time() - start_time > max_retry_elapsed_time: print("SDF generation 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))