Tweepy 2.0 挂在 ssl do_handshake() 上

dre*_*cko 5 python ubuntu ssl tweepy

我有一个长期运行的 Twitter 抓取脚本,偶尔会挂起,堆栈跟踪以

"/usr/lib/python2.7/ssl.py", line 305: self._sslobj.do_handshake()
Run Code Online (Sandbox Code Playgroud)

我的问题是“为什么?” 和“我能做些什么来解决它?”。

在这种情况发生之前,它通常会持续一周左右。最后一次,处理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和到达那里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()
Run Code Online (Sandbox Code Playgroud)

在线程挂起的时候有一些 tweepy 错误。这并不太罕见,尽管这个数字比正常情况略多。不过,第四个看起来很有趣……它发生在线程挂起之前。

  • [Errno 110] : Connection timed out在最后一次followers_ids()调用之前大约 7 分钟(中间有许多各种各样的 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在来自friends_ids()线程的最后一条消息之前大约 2 毫秒,并且在同一个线程中。刚刚收集的好友 id 页面显示一切正常,并且这些调用没有错误。
  • [Errno 104] Connection reset by peersearch线程中,friends_ids线程挂起后大约 17 分钟和线程挂起前将近一个小时search
  • Failed to send request大约 1.5 分钟后无故出现TweepError。
  • 在接下来的 45 分钟内Failed to send request,还有3 个无理由的和 1[Errno 104] Connection reset by peer个。
  • 在线程最终挂起之前,大约有 15 分钟没有错误,search并且有很多lookup_users调用search

dre*_*cko 1

问题似乎是 tweepy 没有实现超时。在最新版本的 tweepy 中,这个问题已经得到解决,并且此后(在连续几个月的数据收集中)再也没有出现过这个问题。