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

问题描述:

我的节俭客户端当前没有连接,所以self.transport.open()的调用应该超时,我的程序执行应该继续,因为它的确如此。但我想知道为什么超时引发异常,为什么堆栈跟踪打印到控制台,以及这是否表明存在问题。为什么我的控制台显示从try块中引发的Thrift异常的堆栈跟踪?

代码:

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... 
+0

程序是否停止?你不在(某种)_Debug_模式,并且异常回溯只显示?显然(在读取输出之后)似乎是这样的:“_root:警告...我要在try块内引发异常!_”。 – CristiFati

+0

程序执行不停止。回溯是在调试模式下和在调试模式下生成的,并且不会生成其他回溯(即使处理了其他异常)。该警告语句来自我 - 请参阅我的代码的try块中的log.warning行。感谢您的期待! –

+1

然后,我认为没有问题。我从来没有使用_thrift_,但我认为它只是显示异常(即使它被捕获),因为它阻止了它做某件事,但我确信这可以通过设置来抑制。 – CristiFati

您使用打印活动记录仪

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)