gpt4 book ai didi

python - 发生错误时自动重启程序

转载 作者:太空宇宙 更新时间:2023-11-03 12:35:22 25 4
gpt4 key购买 nike

程序是这样的:

HEADER CODE
urllib2.initialization()
try:
while True:
urllib2.read(somebytes)
urllib2.read(somebytes)
urllib2.read(somebytes)
...
except Exception, e:
print e
FOOTER CODE

我的问题是当发生错误时(超时、连接被对等方重置等),如何从 urllib2.initialization() 而不是现有的主程序重新启动并再次从 HEADER CODE 重新启动?

最佳答案

你可以将你的代码包装在一个“while not done”循环中:

#!/usr/bin/env python

HEADER CODE
done=False
while not done:
try:
urllib2.initialization()
while True:
# I assume you have code to break out of this loop
urllib2.read(somebytes)
urllib2.read(somebytes)
urllib2.read(somebytes)
...
except Exception, e: # Try to be more specific about the execeptions
# you wish to catch here
print e
else:
# This block is only executed if the try-block executes without
# raising an exception
done=True
FOOTER CODE

关于python - 发生错误时自动重启程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1611256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com