我有一个长时间运行的 Twitter 抓取脚本,偶尔会挂起,堆栈跟踪以结尾
"/usr/lib/python2.7/ssl.py", line 305: self._sslobj.do_handshake()
我的问题是“为什么?” 和“我能做些什么来解决它?”。
在这种情况发生之前,它通常会持续一周左右。这最后一次,处理tweepy
挂起的 4 个线程中有 3 个(第四个正在等待来自挂起线程的信息)。奇怪的是,线程挂起之间有相当长的延迟:首先,线程调用api.followers_ids()
挂起,然后大约 12 分钟后线程调用api.friends_ids()
挂起,然后 1 小时 12 分钟后(!)线程调用api.search()
挂起。在所有这些之间有许多 api 调用。
我有一些代码可以在我发送QUIT
信号时转储它的堆栈跟踪,并且对于挂起的线程,我得到了类似以下的内容。它们与(包括)第二个条目(tweepy/binder.py, line 185, in _call
部分)相同。另外两个从tweepy/cursor.py, line 85 in next
and到达那里tweepy/cursor.py, line 60, in next
:
File "myTwitterScrapingScript.py", line 245, in checkStatus
status = api.rate_limit_status()
File "/scratch/bin/python-virtual-environments/tweepy-2/local/lib/python2.7/site-packages/tweepy/binder.py", line 185, in _call
return method.execute()
File "/scratch/bin/python-virtual-environments/tweepy-2/local/lib/python2.7/site-packages/tweepy/binder.py", line 146, in execute
conn.request(self.method, url, headers=self.headers, body=self.post_data)
File "/usr/lib/python2.7/httplib.py", line 958, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python2.7/httplib.py", line 992, in _send_request
self.endheaders(body)
File "/usr/lib/python2.7/httplib.py", line 954, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 814, in _send_output
self.send(msg) File "/scratch/bin/python-virtual-environments/tweepy-2/local/lib/python2.7/site-packages/tweepy/binder.py", line 185, in _call
return method.execute()
File "/scratch/bin/python-virtual-environments/tweepy-2/local/lib/python2.7/site-packages/tweepy/binder.py", line 146, in execute
conn.request(self.method, url, headers=self.headers, body=self.post_data)
File "/usr/lib/python2.7/httplib.py", line 958, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python2.7/httplib.py", line 992, in _send_request
self.endheaders(body)
File "/usr/lib/python2.7/httplib.py", line 954, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 814, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 776, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 1161, in connect
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)
File "/usr/lib/python2.7/ssl.py", line 381, in wrap_socket
ciphers=ciphers)
File "/usr/lib/python2.7/ssl.py", line 143, in __init__
self.do_handshake()
File "/usr/lib/python2.7/ssl.py", line 305, in do_handshake
self._sslobj.do_handshake()
线程挂起的时间有一些 tweepy 错误。这并不算太不寻常,尽管这个数字略高于正常水平。虽然第四个看起来很有趣..它发生在那个线程挂起之前。
[Errno 110] : Connection timed out
在最后一次调用之前大约 7 分钟followers_ids()
(中间有许多不同的 api 调用)[Errno 104] Connection reset by peer
大约 3 分钟后(再次,几次成功的通话之间)[Errno 110] Connection timed out
friends_ids()
大约在最后一次通话前 1.5 分钟。这是在api.search()
线程中,该线程在第一个线程挂起前大约 5 分钟一直在等待 - 总共等待大约 15 分钟。[Errno 104] Connection reset by peer
在来自线程的最后一条消息之前大约 2 毫秒friends_ids()
,并且在同一个线程中。刚刚收集的朋友 ID 页面看起来一切正常,并且这些调用没有错误。[Errno 104] Connection reset by peer
在search
线程中,线程挂起后大约 17 分钟,friends_ids
线程挂起前将近一个小时search
。Failed to send request
大约 1.5 分钟后无故出现TweepError。- 在接下来的 45 分钟内,还有3 个无缘无故
Failed to send request
的和一个。[Errno 104] Connection reset by peer
- 在线程最终挂起之前
search
,大约有 15 分钟没有错误,有很多lookup_users
电话。search