无法在App Engine上捕获MySQLdb.OperationalError

Tza*_*ach 6 python mysql django google-app-engine mysql-python

我在Google App Engine上运行了一个Django应用程序.DB不时会引发OperationalError正常情况,但是我的代码虽然使用了try..except,却没有捕获异常(我需要这个用于重试目的).

这是我的代码:

from MySQLdb import OperationalError, DatabaseError

DB_RETRY_EXCEPTIONS = (
    OperationalError,
    DatabaseError,
)

class MyClassView(rest_framework.views.APIView):
    @retry(DB_RETRY_EXCEPTIONS, tries=5, delay=5, logger=logger)
    def dispatch(self, *args, **kwargs):
        try:
            return super(MyClassView, self).dispatch(*args, **kwargs)
        except DB_RETRY_EXCEPTIONS as exp:
            logger.warning("Got %s", exp)
            raise
Run Code Online (Sandbox Code Playgroud)

在异常堆栈跟踪中,我可以看到流程正在通过这段代码,但没有警告.

这是堆栈跟踪的最后几行:

File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/MySQLdb-1.2.4b4/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/MySQLdb-1.2.4b4/MySQLdb/connections.py", line 190, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 38")
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

Tza*_*ach 6

原来还有一个OperationalErrordjango.db.utils.OperationalError,那是那个被提出来的。解决方案是也捕获这个异常。