import random import http import time import httplib2 from googleapiclient.errors import HttpError # Explicitly tell the underlying HTTP transport library not to retry, since we are handling retry logic ourselves. httplib2.RETRIES = 1 # Maximum number of times to retry before giving up. MAX_RETRIES = 10 # Always retry when these exceptions are raised. RETRIABLE_EXCEPTIONS = ( http.HTTPStatus.INSUFFICIENT_STORAGE, httplib2.HttpLib2Error, IOError, http.client.NotConnected, http.client.IncompleteRead, http.client.ImproperConnectionState, http.client.CannotSendRequest, http.client.CannotSendHeader, http.client.ResponseNotReady, http.client.BadStatusLine) # Always retry when an HttpError with one of these status codes is raised. RETRIABLE_STATUS_CODES = (500, 502, 503, 504) def resumable_upload(insert_request): response = None error = None retry = 0 while response is None: try: status, response = insert_request.next_chunk() if 'id' in response: return response['id'] except HttpError as err: if err.resp.status in RETRIABLE_STATUS_CODES: error = True else: raise except RETRIABLE_EXCEPTIONS: error = True if error: retry += 1 if retry > MAX_RETRIES: raise Exception('Maximum retry are fail') sleep_seconds = random.random() * 2 ** retry time.sleep(sleep_seconds)