我在找这样的东西:
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);
}
}来自这篇文章:Cleanest way to write retry logic?
我在Python中是一个很不错的人,我知道如果有人有一些提示的话,这可能会很好。这是用于测试代码,它经常出现,但很少被优雅地处理。
发布于 2016-04-02 03:35:18
您可以这样做,添加异常处理和其他您喜欢的提示:
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)https://stackoverflow.com/questions/36367757
复制相似问题