mongoengine 与 gevent,如何关闭/结束连接?
当尝试在 gevent 进程中执行 MyApp.objects.get(foo=bar)
时,我收到 connection returned due to too much open Connections: 204
。在 models.py 中,我有 connect('my_db')
。在我的脚本中,我执行gevent.monkey.patch_all()
我看到了这个问题
pymongo + gevent:扔给我一根香蕉,然后只给 Monkey_patch?
以及这个问题
https://github.com/hmarr/mongoengine/issues/272
但我不了解如何访问连接以调用 end_request
。我也尝试过这个补丁,但它对我没有帮助(除非我使用错误)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎是使用 mongoengine 执行此操作的方法(假设您有一个默认连接):
mongoengine.connection.get_connection('default').start_request()
mongoengine.connection.get_connection('default').end_request()
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()
更新:这个问题已于今年早些时候在 pymongo 中得到解决。确保您使用的是最新版本的 pymongo。
原始答案:
显然,您可以通过使用“代理类......将请求排队给工作人员,它们按需分配新的 PyMongo 连接,在连接错误时丢弃它们等并调用目标方法”来解决此问题,如上所述此处:
http://groups.google.com/group/gevent/browse_thread/thread/a423d1a15d83f73c
(请参阅 Antonin Amand 于 2011 年 8 月 31 日发表的帖子以及 Alexey Borzenkov 的回复)
PS 并且,无论如何,MongoDB 人员正在努力解决这个问题:https://jira.mongodb.org/browse/PYTHON-296
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