我是python的新手,我的脚本大约持续3-4个小时,不会保存任何错误消息。什么可能导致这个问题?
这是我的代码:
import time
import urllib.request
import threading
def load():
try:
content = str(urllib.request.urlopen("[URL]").read())
# do sth with content
threading.Timer(0.5, load).start()
except Exception as e:
file = open("Error.txt","w")
file.write(time.strftime("%H:%M:%S\n\n"))
file.write(e.message)
file.close()
threading.Timer(0.5, load).start()
def main(args):
load()
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
这是ubuntu 14.04上的nohup.out文件:
Exception in thread Thread-907:
Traceback (most recent call last):
File "/usr/lib/python3.4/urllib/request.py", line 1182, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/usr/lib/python3.4/http/client.py", line 1125, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.4/http/client.py", line 1163, in _send_request
self.endheaders(body)
File "/usr/lib/python3.4/http/client.py", line 1121, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.4/http/client.py", line 951, in _send_output
self.send(msg)
File "/usr/lib/python3.4/http/client.py", line 886, in send
self.connect()
File "/usr/lib/python3.4/http/client.py", line 863, in connect
self.timeout, self.source_address)
File "/usr/lib/python3.4/socket.py", line 512, in create_connection
raise err
File "/usr/lib/python3.4/socket.py", line 503, in create_connection
sock.connect(sa)
OSError: [Errno 101] Network is unreachable
答案 0 :(得分:0)
Network is unreachable
通常表示您的计算机出现连接问题。也许这是一个间歇性的,短暂的问题,但你永远不会试图检测它并从中恢复。
考虑使用retry
decorator之类的内容,或手动处理。
(我也发现循环获取逻辑的方式相当奇怪。为什么一个简单的单线程循环对你不起作用?)