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

时间:2017-08-31 22:05:26

标签: python python-2.7 flask thrift werkzeug

我的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...

1 个答案:

答案 0 :(得分:1)

使用

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

然后,对于与TSocket相关的记录器,您可以将调试级别设置为关键

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

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