我有一个try循环,它执行一个post函数,这需要一些时间才能工作:
def recalc_proj(proj):
for x in range(0,10):
while True:
try:
newproj = proj.post('docs/recalc')
except someError:
print "another job is running. waiting 10s"
time.sleep(10)
continue
break
print "SUCCESS"
我只想运行newproj = proj.post('docs/recalc')
一次,如果运行没有错误,请退出。
但是,运行上面的功能后,我在屏幕上得到以下输出:
another job is running. waiting 10s
another job is running. waiting 10s
another job is running. waiting 10s
another job is running. waiting 10s
SUCCESS
another job is running. waiting 10s
another job is running. waiting 10s
another job is running. waiting 10s
为什么即使我需要的代码行成功运行,它仍然继续循环?我以为休息意味着要退出for循环?
该break
语句终止当前循环,即while
您的示例中的循环。您也可以break
在print
语句后加上另一个以终止for
循环:
...
print "SUCCESS"
break
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句