python - Gunicorn+flask+pymongo+gevent hangs on initialization -
simple test app:
from gevent import monkey monkey.patch_all() pymongo import connection, mongoclient 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()
works if it's run standalone app:
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
but fails start gunicorn:
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 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 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 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 pid: 9830
some notes:
- it works if gevent turned off (monkey patching , gunicorn worker class)
- it works if db object created per every incoming request
- versions: gevent 1.0, gunicron 18.0, pymongo 2.6.2, flask 0.9, python 2.6.5
i doubt proper way initialize , share database pool. still, can't find anywhere if there other way share object between requests.
ok, same issue as:
https://jira.mongodb.org/browse/python-607
workarounds worked me:
from gevent import monkey monkey.patch_all() unicode('foo').encode('idna') ...
or:
shcheklein@hostname:~$ export gevent_resolver=ares shcheklein@hostname:~$ gunicorn -w 1 -k gevent -t 5 --debug test:app
Comments
Post a Comment