python中的异常处理-ruby重试循环

python中的异常处理-ruby重试循环,python,exception,resque-retry,Python,Exception,Resque Retry,在ruby u use中获得异常后如何在python中重试循环 begin a.each do |s| end rescue sleep 2 retry end 我将使用睡眠时间,只需在获得excpetion后使用重试循环,现在我想在python中执行此操作 我知道异常处理的代码 try: for i in w: #Loop code exception e: 如果出现任何异常,如何重试循环? #如何在发生任何异常时重试循环您可以创建

在ruby u use中获得异常后如何在python中重试循环

begin
    a.each do |s|
    end
rescue
      sleep 2
        retry 
end
我将使用睡眠时间,只需在获得excpetion后使用重试循环,现在我想在python中执行此操作

我知道异常处理的代码

try:
   for i in w:
   #Loop code
exception e:
如果出现任何异常,如何重试循环?
#如何在发生任何异常时重试循环

您可以创建一个函数并调用它:

def my_func():
    try:
        for i in w:
            #Loop code
    except:
        # here take care of exception, try to fix it, when it occurs
        my_funct()  
如果您不想重新开始:

try:
    for i in w:
        #Loop code
    except:
        # here take care of exception, try to fix it, when it occurs

您可以创建一个函数并调用它:

def my_func():
    try:
        for i in w:
            #Loop code
    except:
        # here take care of exception, try to fix it, when it occurs
        my_funct()  
如果您不想重新开始:

try:
    for i in w:
        #Loop code
    except:
        # here take care of exception, try to fix it, when it occurs

您所寻找的似乎是一个递归函数。大概

# retries = 3
def do(x):
    try:
        y = something(x)
    except Exception as err:
        # while retries > 0:
        #    retries -= 1
            do(x)    
    else:
        print(y)

请注意,这将创建一个无止境的重试循环,除非您以某种方式设置了一个类似于我注释掉的限制器。

您所寻找的似乎是一个递归函数。大概

# retries = 3
def do(x):
    try:
        y = something(x)
    except Exception as err:
        # while retries > 0:
        #    retries -= 1
            do(x)    
    else:
        print(y)

请注意,这将创建一个无止境的重试循环,除非您以某种方式设置了一个类似于我注释掉的限制器。

假设循环在1400数组上处理,如果发生错误,函数将从第0个数组开始,对……相反,我想重试该特定的循环数组(1400)这就是为什么我在评论中说“注意异常并修复”的原因,它说循环在1400数组处理,如果发生错误,函数将从第0个数组开始,对……相反,我想重试循环的特定数组(1400),这就是为什么我在评论中说“注意异常并修复”的原因