Blu*_*oon 2 python while-loop try-except
我写了一个函数,应该多次尝试一个函数,直到这个工作.
def Retry(attempts,back_off,value):
for i in range(attempts):
counter = 0
while attempts > counter:
try:
x = function(value)
except:
counter =+ 1
delay = (counter * back_off) + 1
print ('trying again in {} seconds'.format(delay))
sleep(delay)
continue
break
return x
result = Retry(20,2,value)
Run Code Online (Sandbox Code Playgroud)
每次失败的尝试之后都应该是指数增长的时间间隔,即2秒后的第二次尝试,4秒后的第三次尝试,8秒后的第四次尝试,依此类推.问题在于,在我写的函数中,如果第一次尝试失败,我只会得到一系列无限的行:
trying again in 3 seconds
trying again in 3 seconds
trying again in 3 seconds
trying again in 3 seconds
trying again in 3 seconds
trying again in 3 seconds
....
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?为什么循环堆栈在那里?
该
counter =+ 1
Run Code Online (Sandbox Code Playgroud)
应该
counter += 1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
690 次 |
| 最近记录: |