【问题标题】:Twisted adbapi errors and log.err扭曲的 adbapi 错误和 log.err
【发布时间】:2012-07-26 06:01:30
【问题描述】:

我正在调试“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,但这可能不是最好的主意。有没有更好的方法来处理这个问题?

【问题讨论】:

    标签: mysql twisted trial


    【解决方案1】:

    您是否在问即使记录了错误,如何让单元测试通过?试试flushLoggedErrors。如果您要问其他问题,请澄清。

    【讨论】:

    • 是的,这正是我需要的。谢谢!如果它没有在日志中写任何东西会很棒,但我可以忍受这个:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2016-03-07
    • 2016-08-09
    • 1970-01-01
    • 2011-10-21
    相关资源
    最近更新 更多