0

I am trying to create a retry function in Python which is supposed to try to call any other function and in case of error of the called function repeat the call after some time.

My idea was to give any function as argument of my retry function which should look somehow like this:

def retry_function(function, retries):

  for i in range(0, retries):
      try:
          print("Try no.:", str(i))
          function
          break
      except Exception:
          print("It was not succesful, trying again")
          time.sleep(0.4)
          continue

retry_function(some_other_function(..,..,..), 10)

My problem is, that I do not know how to try by this way every other function when they can have completely different types of arguments and the number of arguments. Is there a way to create this universal retry function? Or how should I call retry function with any function?

Thank you for any help

kvetjo
  • 55
  • 4
  • Probably needs args and kwargs. You can read about them online. Pass them to retry_function, and from it to function inside. Pass needed arguments in your retry_function. –  vrnvorona Sep 08 '21 at 10:15
  • 1
    Just `function` inside your function doesn't do anything. You need to *call* the `function()`. And then it's best to pass a callable which already has its parameter bound, instead of accepting `function` and its arguments separately and trying to put them back together. See the duplicate at the top of your question for a solution. – deceze Sep 08 '21 at 10:21

0 Answers0