Closed. This question needs debugging details。它当前不接受答案。












想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

4年前关闭。



Improve this question




我正在调用一个函数,如果尚未加载网页,它将引发异常。我想等待2秒钟,然后重试,直到页面加载完毕。

我尝试这样:
while(True):
    try:
        some_funciont()
        break
    except:
        time.sleep(2)

但它在第一次迭代后会逸出。

如果没有引发异常,该如何逃生?

最佳答案

尝试这样的事情:

def some_function(){
    try:
        #logic to load the page. If it is successful, it will not go to except.
        return True
    except:
        #will come to this clause when page will throw error.
        return False
    }

while(True):
    if some_function():
        break
    else:
        time.sleep(2)
        continue

09-10 07:42
查看更多