【问题标题】:exception handling - ruby retry loop in python异常处理-python中的ruby重试循环
【发布时间】:2015-04-07 04:09:13
【问题描述】:

在你使用的 ruby​​ 中获取异常后如何在 python 中重试循环

begin
    a.each do |s|
    end
rescue
      sleep 2
        retry 
end

我将使用睡眠时间并在获得异常后使用重试循环现在我想在 python 中执行此操作

我知道异常处理代码

try:
   for i in w:
   #Loop code
exception e:

如果出现异常,如何重试循环? #出现异常如何重试循环

【问题讨论】:

    标签: python exception resque-retry


    【解决方案1】:

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

    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
    

    【讨论】:

    • 如果发生错误,可以说循环正在 1400 数组处处理,函数将从第 0 个数组开始...而不是我想重试该特定的循环数组(1400)
    • 这就是为什么我在评论中说要处理异常并修复它
    【解决方案2】:

    您正在寻找的是一个递归函数。类似的,

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      相关资源
      最近更新 更多