mongoengine with gevent, how to close/end connections?
-
10-02-2021 - |
Domanda
I'm getting connection refused because too many open connections: 204
when trying to do MyApp.objects.get(foo=bar)
in a gevent process. In models.py I have connect('my_db')
. In my script I do gevent.monkey.patch_all()
I've seen this question
pymongo + gevent: throw me a banana and just monkey_patch?
and this issue
https://github.com/hmarr/mongoengine/issues/272
but I don't understand how to get access to the connection to call end_request
. I've also tried this patch but it doesn't help me (unless I'm using it wrong)
Soluzione
This seems to be the way to do it with mongoengine (assuming you have one, default connection):
mongoengine.connection.get_connection('default').start_request()
mongoengine.connection.get_connection('default').end_request()
Altri suggerimenti
UPDATE: This issue was resolved in pymongo earlier this year. Make sure you're using the latest version of pymongo.
Original Answer:
Apparently, you can resolve this issue by using a "proxy class ... that queues requests to workers, they allocate a new PyMongo connections on demand, discard them on connection errors, etc. and call target methods," as described here:
http://groups.google.com/group/gevent/browse_thread/thread/a423d1a15d83f73c
(See the Aug 31, 2011 post by Antonin Amand and the response by Alexey Borzenkov)
P.S. And, in any case, the MongoDB folks are working on resolving the issue: https://jira.mongodb.org/browse/PYTHON-296