2017-08-31 23 views
0

我的節儉客戶端當前沒有連接,所以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

回答

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) 
相關問題