mongoengine 与 gevent,如何关闭/结束连接?

发布于 2024-12-09 18:54:57 字数 768 浏览 0 评论 0原文

当尝试在 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。我也尝试过这个补丁,但它对我没有帮助(除非我使用错误)

https:// /gist.github.com/1184264

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)

https://gist.github.com/1184264

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒冷纷飞旳雪 2024-12-16 18:54:57

这似乎是使用 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()

-小熊_ 2024-12-16 18:54:57

更新:这个问题已于今年早些时候在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文