Python 如何在我的代码工作时打破for并尝试循环?
我有一个try循环,它执行一个post函数,这需要一些时间:Python 如何在我的代码工作时打破for并尝试循环?,python,for-loop,while-loop,try-catch,Python,For Loop,While Loop,Try Catch,我有一个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. waiti
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
为什么即使在我需要的代码行成功运行之后,它仍会继续循环?我认为break意味着退出for循环?语句终止当前循环,即示例中的
while
循环。您也可以在print
语句之后放置另一个break
,以终止for
循环:
...
print "SUCCESS"
break