我正在调试“MySQL 服务器已消失”错误。有一个建议的解决方案,或多或少有效:
from twisted.enterprise import adbapi
from twisted.python import log
import MySQLdb
class ReconnectingConnectionPool(adbapi.ConnectionPool):
"""Reconnecting adbapi connection pool for MySQL.
This class improves on the solution posted at
http://www.gelens.org/2008/09/12/reinitializing-twisted-connectionpool/
by checking exceptions by error code and only disconnecting the current
connection instead of all of them.
Also see:
http://twistedmatrix.com/pipermail/twisted-python/2009-July/020007.html
"""
def _runInteraction(self, interaction, *args, **kw):
try:
return adbapi.ConnectionPool._runInteraction(self, interaction, *args, **kw)
except MySQLdb.OperationalError, e:
if e[0] not in (2006, 2013):
raise
log.msg("RCP: got error %s, retrying operation" %(e))
conn = self.connections.get(self.threadID())
self.disconnect(conn)
# try the interaction again
return adbapi.ConnectionPool._runInteraction(self, interaction, *args, **kw)
取自这里: http ://www.gelens.org/2009/09/13/twisted-connectionpool-revisited/
但是,异常不仅在我们的 ReconnectingConnectionPool 中被捕获,而且在 _runInteraction 本身中也被捕获。它会触发 log.err,它(显然)做了两件事:
- 打印错误 - 虽然我们的应用程序工作正常
- 更糟糕的是,它使试验失败,因此测试失败。我不确定这是 log.err 问题,但它仍然会发生。
我可以破解 adbapi,但这可能不是最好的主意。有没有更好的方法来处理这个问题?