gUnicorn / gevent:请求库抛出RecursionError:超出最大递归深度

时间:2018-10-11 15:55:40

标签: python flask gunicorn gevent

我有一个使用gunicorn和gevent部署的Flask应用。我已经放了

import gevent.monkey
gevent.monkey.patch_all()

在app.py文件的顶部。

但是我仍然收到以下警告和错误:

MonkeyPatchWarning: Monkey-patching ssl after ssl has already been imported may lead to errors, including RecursionError on Python 3.6. It may also silently lead to incorrect behaviour on Python 3.7. Please monkey-patch earlier. See https://github.com/gevent/gevent/issues/1016. Modules that had direct imports (NOT patched): ['requests.packages.urllib3.util


  File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/requests/api.py", line 110, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/requests/sessions.py", line 596, in send
    r = adapter.send(request, **kwargs)
  File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 595, in urlopen
    chunked=chunked)
  File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 352, in _make_request
    self._validate_conn(conn)
  File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 831, in _validate_conn
    conn.connect()
  File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/requests/packages/urllib3/connection.py", line 289, in connect
    ssl_version=resolved_ssl_version)
  File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/requests/packages/urllib3/util/ssl_.py", line 291, in ssl_wrap_socket
    ciphers=ciphers)
  File "/home/travis/virtualenv/python3.6.3/lib/python3.6/site-packages/requests/packages/urllib3/util/ssl_.py", line 254, in create_urllib3_context
    context.options |= options
  File "/opt/python/3.6.3/lib/python3.6/ssl.py", line 465, in options
    super(SSLContext, SSLContext).options.__set__(self, value)
  File "/opt/python/3.6.3/lib/python3.6/ssl.py", line 465, in options
    super(SSLContext, SSLContext).options.__set__(self, value)
  File "/opt/python/3.6.3/lib/python3.6/ssl.py", line 465, in options
    super(SSLContext, SSLContext).options.__set__(self, value)
  [Previous line repeated 306 more times]
RecursionError: maximum recursion depth exceeded

1 个答案:

答案 0 :(得分:0)

我们遇到了类似的问题,发现我们用 ddtrace-run(datadog tracer) 包装了 gunicorn,而 ddtrace-run 甚至在 gevent 可以对它进行猴子补丁之前就调用了 urllib3。有关该错误的更多详细信息,请访问 dd-trace-py/issues/506。为了解决这个问题,我们升级到最新版本的 ddtrace-run。

ddtrace-run gunicorn -b 0.0.0.0:${PORT} --log-level=${GUNICORN_LOG_LEVEL} ... 'app.main:main()'

如果您在 app.py 顶部添加了 gevent.monkey.patch_all() ,请仔细检查您是如何调用 gunicorn 入口点的。在 gevent 和调用 SSL 之前会有一些事情发生。