简体   繁体   English

Gunicorn RecursionError 与 gevent 和 python 3.6.2 中的请求

[英]Gunicorn RecursionError with gevent and requests in python 3.6.2

I have a simple flask script that uses requests to make http requests to third party web service.我有一个简单的烧瓶脚本,它使用请求向第三方 Web 服务发出 http 请求。 The way I run the script in gunicorn is我在 gunicorn 中运行脚本的方式是

gunicorn abc:APP -b 0.0.0.0:8080 -w 4 -k gevent --timeout 30 --preload

However, after I upgrade the code to python 3.6.2, I can still run the server, but whenever the webserver received a request, it shows但是,在我将代码升级到 python 3.6.2 后,我仍然可以运行服务器,但是每当网络服务器收到请求时,它就会显示

RecursionError: maximum recursion depth exceeded while calling a Python object

on every worker, and the server seems are still running.在每个工人身上,服务器似乎仍在运行。 When I change the running command to当我将运行命令更改为

gunicorn abc:APP -b 0.0.0.0:8080 -w 4 --timeout 30 --preload

It all works again.这一切都再次起作用。 So is there any issue with gunicorn's async worker and requests in python 3.6.2?那么gunicorn的异步工作者和python 3.6.2中的请求有什么问题吗? Is there a way to fix this?有没有办法来解决这个问题?

(This question is also asked at https://github.com/benoitc/gunicorn/issues/1559 ) (这个问题也在https://github.com/benoitc/gunicorn/issues/1559提出)

This is because of the ssl is imported before monkey patch, gunicorn import the ssl module(config.py in gevent) when loading config,however the monkey patch is called when init the worker or at the top of app.py file which is definitely after import of ssl, this is why we get the warning.这是因为ssl是在monkey patch之前导入的,gunicorn在加载config时会导入ssl模块(gevent中的config.py),但是monkey patch是在初始化worker时或者app.py文件的顶部调用的,这肯定是导入 ssl 后,这就是我们收到警告的原因。

A simple solution is to use a config file for gunicorn.一个简单的解决方案是使用 gunicorn 的配置文件。 we could do gevent monkey patch at the beginning of config file, and start gunicorn with the config file, in this way, the monkey patch could be completed before import ssl, and thus avoid this problem.我们可以在配置文件的开头打gevent monkey patch,然后用配置文件启动gunicorn,这样就可以在import ssl之前完成monkey patch,从而避免这个问题。

A config file named gunicorn_config.py could contain lines below:名为 gunicorn_config.py 的配置文件可能包含以下行:

import gevent.monkey
gevent.monkey.patch_all()

workers = 8

and then we could start gunicorn with然后我们可以开始 gunicorn

gunicorn --config config.py --worker-class gevent --preload -b 0.0.0.0:5000 app:app

More information could be found here更多信息可以在这里找到

Please see https://github.com/benoitc/gunicorn/issues/1559 .请参阅https://github.com/benoitc/gunicorn/issues/1559 This may possibly be fixed in the next version of gunicorn, but unfortunately you may have to stay with python 3.5.2 if you don't want to break gunicorn.这可能会在下一版本的 gunicorn 中得到修复,但不幸的是,如果您不想破坏 gunicorn,您可能不得不继续使用 python 3.5.2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM