获取 ServerNotFoundError 时将 gevent/greenlet 与请求一起使用



我正在尝试从远程API检索数据。远程 API 没有任何批量功能,因此对于我需要的每个对象,我必须发出单独的 GET 请求。

我已经将gevent添加到了混合中。它有时效果很好,但是如果我再次尝试同一组请求,则 50 个请求中的 100 个会失败:

Traceback (most recent call last):
  ...
  File "/Users/---/venv/lib/python2.7/site-packages/httplib2/__init__.py", line 1570, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/Users/---/venv/lib/python2.7/site-packages/httplib2/__init__.py", line 1317, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/Users/---/venv/lib/python2.7/site-packages/httplib2/__init__.py", line 1258, in _conn_request
    raise ServerNotFoundError("Unable to find the server at %s" % conn.host)
ServerNotFoundError: Unable to find the server at my.remote.host
<Greenlet at 0x10c6eacd0: function_name(<Object1>, <Object2>, u'zebra', True)> failed with ServerNotFoundError

关于解决这个问题的方法有什么想法吗?这是请求太多太快的结果吗?如果是这样,有没有一种简单的方法来限制绿地的数量?

众所周知,

作为此问题的一部分,Gevent会导致python请求的一些DNS问题(您无意中错误地标记了这些问题)。幸运的是,我们之前见过这个问题,用户像这样解决了它

gevent.dns.resolve_ipv4('example.com')
# However you make your httplib2 call.

考虑到这一点,另一位用户指出,这可能已经在新版本的gevent中修复。

无论上面的代码片段是否适合您,您都应该尝试升级 gevent 以确保它不是这样。

最新更新