Is it fine to retry a web service call like this in Python? Would there be any scenarios the exception might not work the way we expect it? I expect it to continue only if it's fine, otherwise just retry, and if that doesn't work, just throw the error out.
def getblob(html):
num_of_retries = 2
time_interval = 2
while num_of_retries:
try:
request_id = str(uuid.uuid4())
params = [('requestId', request_id)]
url = 'service.com?'
url = url + urllib.urlencode(params)
request = urllib2.Request(url, data=html)
request.add_header('Accept', 'application/pdf')
request.add_header('Content-Type', 'text/html')
handle = urllib2.urlopen(request)
pdf_blob = handle.read()
break
except Exception:
typ, val, tb = sys.exc_info()
logger.error(traceback.format_exception(typ, val, tb))
time.sleep(time_interval)
num_of_retries -= 1
# If there aren't any retries - propogate
if not num_of_retries:
raise
return pdb_blob
I wnated to review this because recently I bumped into an issue where I hit an Internal Server Exception on the server side and the retry gave me an HTTP 200 response with incomplete file (it was a PDF without EOF Marker).
Would this snippet cause anything like this? It does work and I get PDF back as well. However from time to time I hit an HTTP 500 (some unknown server issue) and then HTTP 200 with incomplete blob. The incomplete blob is what makes me investigate if this loop can cause that problem. This HTTP 500 HTTP 200 (Incomplete Blob) is very much a pattern. Before I added this retry, I used to get HTTP 500 but never an incomplete blob.