本文介绍了为什么 PyMongo 会抛出 AutoReconnect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在研究我的 Python Web 应用程序的一些奇怪问题(特别是有关 MongoDB 连接的问题)时,我注意到了一些在 PyMongo 官方文档页面.我的网络应用程序使用 Flask,但这不会影响我面临的问题.

While researching some strange issues with my Python web application (in particular, issues regarding MongoDB connectivity), I noticed something on the official PyMongo documentation page. My web application uses Flask, but this shouldn't influence the issue I'm facing.

PyMongo 驱动程序进行连接池化,但当连接失效并且需要重新连接时,它也会抛出异常 (AutoReconnect).

The PyMongo driver does connection pooling, but it also throws an exception (AutoReconnect) when a connection is stale and a reconnect is due.

它指出(关于 AutoReconnect 异常):

It states that (regarding the AutoReconnect exception):

为了自动重新连接,您必须处理此异常,识别导致它的操作不一定成功.未来的操作将尝试打开一个新的连接到数据库(并将继续引发此异常,直到第一个连接成功).

我注意到这实际上经常发生(而且似乎不是错误).连接由 MongoDB 服务器关闭,看起来像是几分钟的不活动,并且需要由 Web 应用程序重新创建.

I have noticed that this actually happens constantly (and it doesn't seem to be an error). Connections are closed by the MongoDB server after what seems like several minutes of inactivity, and need to be recreated by the web application.

我不明白为什么 PyMongo 驱动程序在重新连接时会抛出错误(驱动程序的用户需要自己处理),而不是透明地执行.(甚至可以有一个用户可以设置的选项,以便 AutoReconnect 异常 do 被抛出,但不明智的默认设置是这些异常不会被抛出全部,并且无缝重新创建连接?)

What I don't understand it why the PyMongo driver throws an error when it reconnects (which the user of the driver needs to handle themselves), instead of doing it transparently. (There could even be an option a user could set so that AutoReconnect exceptions do get thrown, but wouldn't a sensible default be that these exceptions don't get thrown at all, and the connections are recreated seamlessly?)

我在使用其他数据库系统时从未遇到过这种行为,这就是为什么我有点困惑.

I have never encountered this behavior using other database systems, which is why I'm a bit confused.

还值得一提的是,当连接到我的本地开发 MongoDB 服务器时,我的 Web 应用程序的 MongoDB 连接永远不会失败(我认为这与它是本地连接的事实有关,并且连接是通过 UNIX 完成的套接字而不是网络套接字,但我可能是错的).

It's also worth mentioning that my web application's MongoDB connections never fail when connecting to my local development MongoDB server (I assume it would have something to do with the fact that it's a local connection, and that the connection is done through a UNIX socket instead of a network socket, but I could be wrong).

推荐答案

您误解了 AutoReconnect.当驱动程序尝试与服务器通信(发送命令或其他操作)并且发生网络故障或类似问题时,它会引发.异常的名称旨在传达您不必创建 MongoClient 的新实例,现有客户端将在您的应用程序尝试下一个操作时自动尝试重新连接.如果出现同样的问题,则再次引发 AutoReconnect.

You're misunderstanding AutoReconnect. It is raised when the driver attempts to communicate with the server (to send a command or other operation) and a network failure or similar problem occurs. The name of the exception is meant to communicate that you do not have to create a new instance of MongoClient, the existing client will attempt to reconnect automatically when your application tries the next operation. If the same problem occurs, AutoReconnect is raised again.

我怀疑您看到套接字超时(并引发 AutoReconnect)的原因是服务器和您的应用程序之间有一个负载平衡器,它会在一段时间不活动后关闭连接.例如,这显然发生在 Microsoft 的 Azure 平台上 13 分钟没有在套接字上进行活动后.您可以使用 PyMongo 2.8 中添加的 socketKeepAlive 选项来解决此问题.请注意,您还必须将应用程序服务器上的 keepalive 间隔设置为适当的值(Linux 上的默认值为 2 小时).请参阅此处了解更多信息.

I suspect the reason you are seeing sockets timeout (and AutoReconnect being raised) is that there is a load balancer between the server and your application that closes connections after some period of inactivity. For example, this apparently happens on Microsoft's Azure platform after 13 minutes of no activity on a socket. You might be able to fix this by using the socketKeepAlive option, added in PyMongo 2.8. Note that you will also have to set the keepalive interval on your application server to an appropriate value (the default on Linux is 2 hours). See here for more information.

这篇关于为什么 PyMongo 会抛出 AutoReconnect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!