为什么我的远程 MongoDB 连接需要对每个查询进行身份验证?
在与不同的事情进行斗争之后,我终于能够在 Apache 上运行 BottlePY 并运行 MongoDB 驱动的地点。我习惯于运行 Django 应用程序,所以我将在我的问题中涉及到这一点。
问题
每次通过 BottlePY 加载页面时,与位于 MongoHQ.com 上的 MongoDB 数据库的连接都需要重新进行身份验证(这意味着它可能必须重新连接)。
我发现的
我在每个模型函数的顶部附加了一个 db.keep_alive()
函数,以便在运行任何 mongodb 查询之前,它会尝试运行一个简单的查询。如果失败,它会捕获OperationFailure 或AutoReconnect 错误,然后调用db.authenticate()
函数。重新进行身份验证后,我让它向日志数据库添加一条日志,以监视需要重新身份验证的频率。目前,它需要在每次页面加载时重新进行身份验证(这需要运行查询)。这是不对的。
与 Django 的区别
我在 django 中使用了相同的概念,并且发现数据库连接只需要在 10-15 分钟没有运行查询后进行身份验证。
我不明白为什么在 django 中创建 pymongo 连接与在 Bottle 中创建连接不同,因为我使用相同的驱动程序、函数和方法。我也没有使用任何 ORMS 或类似的东西。
版本
- Bottle:0.9.dev
- Django:1.2.1 Final
- PyMongo:1.8
我感谢您的帮助!
更新:一位朋友快速浏览了一下,注意到以下内容可能有助于回答我的问题。
看来每个请求都是 启动一个新的Python进程,如 与 Django 相反,其中单个 进程保持运行很长时间 一段时间。
After fighting with different things here and there, I was finally able to get BottlePY running on Apache and run a MongoDB powered site. I am used to running Django apps, so I will be relating to that a bit in my question.
The Problem
Every time a page is loaded via BottlePY, the connection to the MongoDB database located on MongoHQ.com needs to be re-authenticated (meaning it probably had to reconnect).
What I Found
I attached a db.keep_alive()
function to the top of each model function, so that before any mongodb query is run, it trys to run a simple query. If it fails, it catches the OperationFailure or AutoReconnect errors and then calls the db.authenticate()
function. After it reauthenticates, I have it add a log to a logs db to monitor how often it needs to reauthenticate. Currently, it needs to reauthenticate on every page load (that requires running a query). This isn't right.
Difference from Django
I use this same concept in django, and have found that the db connection only needs to be authenticated after 10-15 minutes of no queries being run.
I don't understand why creating a pymongo connection in django would be different from creating one in bottle, since I am using the same driver, functions and methods. I am not using any ORMS or anything like that either.
Versions
- Bottle: 0.9.dev
- Django: 1.2.1 final
- PyMongo: 1.8
I appreciate the help!
Update: A friend was able to take a quick look and noticed the following that may help with answering my question.
It appears that each request is
launching a new Python process, as
opposed to Django, in which a single
process remains running for a long
period of time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这最终成为 Bottle 和 MongoHQ 之间的一件奇怪的事情。没有找到真正的解决方案,但我无法用其他框架重新创建它。任何其他想法表示赞赏。
This just ended up to be a weird thing between Bottle and MongoHQ. No real solution was found, but I couldn't recreate it with other frameworks. Any other ideas are appreciated.
您的 apache xxx.conf 是否包含类似以下内容:
WSGIDaemonProcess 项目 user=mysite group=www-data 进程=5 个线程=1
WSGIProcessGroup项目
我认为最重要的应该是
threads=1
does your apache xxx.conf contain something like:
WSGIDaemonProcess project user=mysite group=www-data processes=5 threads=1
WSGIProcessGroup project
I think most important should be
threads=1