Home > OS >  Python looping try/except not breaking out of function after success
Python looping try/except not breaking out of function after success

Time:08-05

I'm having difficult understanding why my beneath while/try/except isn't breaking after a success.

def api(url, data, head):
  tries = 6
  for i in range(tries):
      try:
        req = requests.post(url, json.dumps(data), headers=head, timeout=3)
        req.raise_for_status()
      except requests.exceptions.RequestException as err:
          if i < tries - 1: # i is zero indexed
              continue
          else:
              raise
      break

The api function is being called within another function:

a():
    api(url1, data1, head)
    api(url2, data2, head)

The loop works as intended and I receive a 200 status typically on the first POST call. Once succeeded, it doesn't exit the function and continue in function a() in which it was being called from.

I have also made use of a similar while/try/except function as shown beneath but I'm receiving the same issue.

def api(url, data, head):
  retries = 0
  success = False
  while not success and retries < 7:
    try:
      req = requests.post(url, json.dumps(data), headers=head, timeout=3)
      req.raise_for_status()
      success = True
      break
    except requests.exceptions.RequestException as err:
      wait = retries * 15;
      logging.info(err)
      logging.info("Issue with the request, retrying")
      time.sleep(wait)
      retries  = 1

Any help would be greatly appreciated. I understand I am not making correct use of the continue/break/raise clauses. I have tried looking into this but I'm getting nowhere.

Thanks.

CodePudding user response:

The beneath solution works as expected

def api(url, data, head):
  tries = 6
  for i in range(tries):
      try:
        req = requests.post(url, json.dumps(data), headers=head, timeout=3)
        return req
      except requests.exceptions.RequestException as err:
          if i < tries - 1: # i is zero indexed
              continue
          else:
              raise

CodePudding user response:

Your back-off strategy could be improved by utilising the retry mechanism available within the requests module.

You also need to think about what should happen if the number of retries are exhausted.

Maybe this will improve things:

import requests
import json
import time
import logging

RETRIES = 7


def api(url, data, head):
    last_err = None
    for r in range(RETRIES):
        try:
            requests.post(url, json.dumps(data), headers=head, timeout=3).raise_for_status()
            break
        except requests.exceptions.RequestException as err:
            last_err = err
            logging.info(err)
            logging.info("Issue with the request, retrying")
            time.sleep(r*15)
    else:
        raise last_err
  • Related