有没有办法在发生故障的情况下使用Python的tqdm模块进行最后一次迭代,例如异常?我在文档或Google上没有找到任何内容。
我想得到最后一次迭代,所以我可以从发生异常的地方开始循环(在我的情况下是超时错误)。
我虽然有这样的事情:
def loop_and_call_external_service(list, start_at=0):
if start_at > 0:
# cut the list using start_at index
for i in tqdm(list):
call_a_service_that_might_timeout(list.id)
retry_attempts = 0
try:
loop_and_call_external_service(list, 0)
except SomeService.TimeoutException:
if retry_attempts <= 3:
retry_attempts += 1
last_index = tqdm._last_iteration_pointer
loop_and_call_external_service(list, last_index)
else:
exit()
exception AnythingElse:
exit()