0

I was looking for something just like this:

public static class Retry
{
   public static void Do(
       Action action,
       TimeSpan retryInterval,
       int retryCount = 3)
   {
       Do<object>(() => 
       {
           action();
           return null;
       }, retryInterval, retryCount);
   }

   public static T Do<T>(
       Func<T> action, 
       TimeSpan retryInterval,
       int retryCount = 3)
   {
       var exceptions = new List<Exception>();

       for (int retry = 0; retry < retryCount; retry++)
       {
          try
          { 
              if (retry > 0)
                  Thread.Sleep(retryInterval);
              return action();
          }
          catch (Exception ex)
          { 
              exceptions.Add(ex);
          }
       }

       throw new AggregateException(exceptions);
   }
}

from this post: Cleanest way to write retry logic?

I am a decent enough in Python to know that this could be really nice if someone has some tips. This is for test code where this comes up very often but is rarely handled elegantly.

Community
  • 1
  • 1
chrismead
  • 2,163
  • 3
  • 24
  • 36
  • 1
    This [**retry**](https://pypi.python.org/pypi/retry/) package might just be what you want. – Anzel Apr 02 '16 at 00:21

1 Answers1

0

You can do something like this, adding exception handling and other bells and whistles as you please:

def retry_fn(retry_count, delay, fn, *args, *kwargs):
    retry = True
    while retry and retry_count:
        retry_count -= 1
        success, results = fn(*args, **kwargs):
        if success or not retry_count:
            return results
        time.sleep(delay)
pyInTheSky
  • 1,459
  • 1
  • 9
  • 24