Gunicorn+flask+pymongo+gevent hangs on initialization
简单测试应用程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from gevent import monkey monkey.patch_all() from pymongo import Connection, MongoClient from flask import Flask, make_response app = Flask(__name__) print"connect" connection = MongoClient("host1, host2, host3", 27017, max_pool_size=4, **{"connectTimeoutMS": 3000,"socketTimeoutMS": 3000,"use_greenlets": True}) print"db" db = connection.barn_2 @app.route('/') def hello_world(): return make_response("Hello world!", 200, {'Content-type': 'application/json; charset=UTF-8'}) if __name__ == '__main__': app.run() |
如果它是作为独立应用程序运行的,那么它就可以很好地工作:
1 2 3 4 5 6 | shcheklein@hostname:~$ python test.py connect db * Running on http://127.0.0.1:5000/ 127.0.0.1 - - [07/Apr/2014 13:07:31]"GET / HTTP/1.1" 200 - ^CKeyboardInterrupt |
但未能从Gunicorn开始:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | shcheklein@hostname:~$ gunicorn -w 1 -k gevent -t 5 --debug test:app 2014-04-07 13:15:04 [9752] [INFO] Starting gunicorn 18.0 2014-04-07 13:15:04 [9752] [INFO] Listening at: http://127.0.0.1:8000 (9752) 2014-04-07 13:15:04 [9752] [INFO] Using worker: gevent 2014-04-07 13:15:04 [9757] [INFO] Booting worker with pid: 9757 connect 2014-04-07 13:15:09 [9752] [CRITICAL] WORKER TIMEOUT (pid:9757) 2014-04-07 13:15:09 [9752] [CRITICAL] WORKER TIMEOUT (pid:9757) 2014-04-07 13:15:10 [9787] [INFO] Booting worker with pid: 9787 connect 2014-04-07 13:15:15 [9752] [CRITICAL] WORKER TIMEOUT (pid:9787) 2014-04-07 13:15:15 [9752] [CRITICAL] WORKER TIMEOUT (pid:9787) 2014-04-07 13:15:16 [9809] [INFO] Booting worker with pid: 9809 connect 2014-04-07 13:15:21 [9752] [CRITICAL] WORKER TIMEOUT (pid:9809) 2014-04-07 13:15:21 [9752] [CRITICAL] WORKER TIMEOUT (pid:9809) 2014-04-07 13:15:22 [9830] [INFO] Booting worker with pid: 9830 |
一些注释:
- 如果关闭gevent(猴子修补和Gunicorn工人班),它会很好地工作。
- 如果每个传入请求都创建了db对象,那么它就会工作。
- 版本:gevent 1.0、gunircon 18.0、pymongo 2.6.2、flask 0.9、python 2.6.5
我怀疑这是初始化和共享数据库池的正确方法。不过,如果有其他方法可以在请求之间共享对象,我还是找不到任何地方。
好吧,这是同一个问题:
https://jira.mongodb.org/browse/python-607
对我有用的解决方法:
1 2 3 4 5 6 | from gevent import monkey monkey.patch_all() unicode('foo').encode('idna') ... |
或:
1 2 | shcheklein@hostname:~$ export GEVENT_RESOLVER=ares shcheklein@hostname:~$ gunicorn -w 1 -k gevent -t 5 --debug test:app |
也许这个古尼康问题可以为您提供一些额外的信息和帮助。