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

时间:2013-10-16 20:07:34

标签: python gevent httplib2 greenlets

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

我已将gevent添加到混音中。它有时很好用,但是如果我再次尝试相同的请求集,那么100个中的50个将失败:

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

有关如何解决此问题的任何想法?这是太多请求太快的结果吗?如果是这样,是否有一种简单的方法来限制greenlets的数量?

1 个答案:

答案 0 :(得分:1)

Gevent已知会导致一些DNS问题与python请求(你无意中和错误地标记)作为这个问题的一部分。幸运的是,我们之前已经看过这个问题而用户resolved it就像这样

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

考虑到这一点,另一位用户注意到了此may already be fixed in a new version of gevent

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