实施重试例程

时间:2018-09-20 07:02:29

标签: python python-3.x

我有以下想法。是否可以在python中实现重试例程?这是我所做的一个简单示例。我想有一个更灵活的解决方案。与功能无关。因此,请使用其他任何功能切换removeFile并摆脱main中的while循环。

import os
import time

def removeFile(file):

    try:
        os.remove(file)
        print("removed : "+file)
        return True
    except PermissionError:
        print("could not delete file "+file+" ; will try again")
        return False


if __name__ == "__main__":
    file = "some_path/file.ext"

    sucess = False
    maxCount = 5
    count = 0
    while not sucess:
        sucess = removeFile(file)
        count += 1
        if count == maxCount:
            sucess = True
            print("could not delete file "+file+" ; permission denied.")
        time.sleep(5)

3 个答案:

答案 0 :(得分:2)

由于@shmee,我有了使用装饰器的新方法。

def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None):
    """Retry calling the decorated function using an exponential backoff.

    http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
    original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry

    :param ExceptionToCheck: the exception to check. may be a tuple of
        exceptions to check
    :type ExceptionToCheck: Exception or tuple
    :param tries: number of times to try (not retry) before giving up
    :type tries: int
    :param delay: initial delay between retries in seconds
    :type delay: int
    :param backoff: backoff multiplier e.g. value of 2 will double the delay
        each retry
    :type backoff: int
    :param logger: logger to use. If None, print
    :type logger: logging.Logger instance
    """
    def deco_retry(f):

        @wraps(f)
        def f_retry(*args, **kwargs):
            mtries, mdelay = tries, delay
            while mtries > 1:
                try:
                    return f(*args, **kwargs)
                except ExceptionToCheck:
                    msg = "%s, Retrying in %d seconds..." % (str(ExceptionToCheck), mdelay)
                    if logger:
                        #logger.exception(msg) # would print stack trace
                        logger.warning(msg)
                    else:
                        print(msg)
                    time.sleep(mdelay)
                    mtries -= 1
                    mdelay *= backoff
            return f(*args, **kwargs)

        return f_retry  # true decorator

    return deco_retry  

现在剩下要做的就是装饰我们的小删除功能:

 @retry(PermissionError, tries=5, delay=2,backoff=2)
    def removeFile(f):
        os.remove(f)

答案 1 :(得分:0)

只需将while循环放入方法

import os
import time

def removeFile(file):
    sucess = False
    maxCount = 5
    count = 0

    while not sucess:
        try:
            os.remove(file)
            print("removed : "+file)
            sucess = True
        except PermissionError:
            print("could not delete file "+file+" ; will try again")
            sucess =  False
            count += 1
        if count == maxCount:
            sucess = True
            print("could not delete file "+file+" ; permission denied.")
        time.sleep(5)



if __name__ == "__main__":
    file = "some_path/file.ext"

    removeFile(file)

答案 2 :(得分:0)

我使用此通用模板代码在工作中重试,以处理所有情况并避免代码失败。

N_try = 10

for n_try in range(0,N_try):
    success = None
    try:
        #code execution
        output = None
        output = CodeExecution(input)
    except ex_name_1 as (errno, strerror):
        success = False
        print("EXCEPTION CAUGHT: Try {} of {}:".format(n_try, N_try))
        print("exc_n={}, exc_str={}".format(errno, strerror))
    except ex_name_2 as (errno, strerror):
        success = False
        print("EXCEPTION CAUGHT: Try {} of {}:".format(n_try, N_try))
        print("exc_n={}, exc_str={}".format(errno, strerror))
    except:
        print("UNKNOWN EXCEPTION CAUGHT: Try {} of {}".format(n_try, N_try))
        success = False

    if success is True:
        if CheckOutpup(output) is True:
            success = True
            break
        else:
            success = False
            print("CHECK OUTPUT: Try {n_try} of {N_try} FAILED".format{n_try, N_try})
    else:
        print("EXECUTION: Try {n_try} of {N_try} FAILED".format{n_try, N_try})
        success = False

if success is False:
    print("Failed execution after {N_try} tries".format{N_try})