为什么我的控制台显示来自 try 块中引发的 Thrift 异常的堆栈跟踪?

Why does my console display a stack trace from a Thrift exception raised within a try block?

我的 thrift 客户端当前未连接,因此对 self.transport.open() 的调用应该超时,我的程序应该继续执行。但我想知道为什么超时会引发异常,为什么堆栈跟踪会打印到控制台,以及这是否表示有问题。

代码:

def connect(self, url=None):
    """Attempt to connect to supervisor Thrift server"""
    if conf.TESTING:
        return

    try:
        self.url = url if url is not None else self.url
        self.socket = TSocket.TSocket(self.url, constants.kSupervisorPort)
        self.socket.setTimeout(1000)
        self.transport = TTransport.TBufferedTransport(self.socket)
        self.protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
        self.client = Supervisor.Client(self.protocol)
        log.warning("...Im about to raise an exception from inside a try block!")
        self.transport.open()
        self.connected = True
        log.info('[TaskStateClient] Connected at url: ' + self.url)

    except Exception:
        log.warning("...and now Im handling the raised exception...")

和堆栈跟踪:

->  Running
python app.py
werkzeug    : INFO      * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
root        : WARNING  ...Im about to raise an exception from inside a try block!
thrift.transport.TSocket: INFO     Could not connect to ('192.168.1.219', 9002)
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/thrift/transport/TSocket.py", line 104, in open
handle.connect(sockaddr)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
timeout: timed out
thrift.transport.TSocket: ERROR    Could not connect to any of [('192.168.1.219', 9002)]
root        : WARNING  ...and now Im handling the raised exception...

您使用

打印活动记录器
import logging
for key in logging.Logger.manager.loggerDict:
    print(key)

然后对于TSocket相关的logger,可以将debug级别设置为critical

logging.getLogger('thrift.transport.TSocket').setLevel(logging.CRITICAL)

logging.getLogger('thrift.transport.TSocket').setLevel(logging.NOTSET)