Skip to content

Instantly share code, notes, and snippets.

@feczo
Created August 27, 2014 03:49
Show Gist options
  • Save feczo/bfd9991e4f84dffbc661 to your computer and use it in GitHub Desktop.
Save feczo/bfd9991e4f84dffbc661 to your computer and use it in GitHub Desktop.
Execute BigQuery request with retry
def ExecuteRequestWithRetries(request, num_tries=5):
"""Executes a request and retries certain failures.
Failures are retried if they are in the list of errors to retry.
List includes system errors(500, 503) and an
authentication error(401)
Args:
request: The request to issue to big query. It must be an object with
an execute method.
num_tries: The number of times to attempt the request.
Returns:
The results of the request.
"""
for _ in xrange(num_tries -1):
try:
return request.execute()
except HttpError as e:
if e.resp['status'] not in [401, 500, 503]:
raise
return request.execute()
bigquery_service = build('bigquery', 'v2', http=http)
jobCollection = bigquery_service.jobs()
current_job = ExecuteRequestWithRetries(
jobCollection.get(projectId = PROJECT_NUMBER, jobId = bq_job_id))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment