2016-07-19 167 views
1

我有一個功能。該函數在一些更多的線程中開始。我試圖打印自己的錯誤消息。但是,我所做的並不重要,我仍然得到印刷的回溯。我的功能:Python:爲什麼traceback打印?

def getSuggestengineResultForThree(suggestengine, seed, dynamoDBLocation): 
    results[seed][suggestengine] = getsuggestsforsearchengine(seed, suggestengine) 

    for keyword_result in results[seed][suggestengine]: 
     o = 0 
     while True: 
      try: 
       allKeywords.put_item(
        Item={ 
         'keyword': keyword_result 
        } 
       ) 
       break 
      except ProvisionedThroughputExceededException as pe: 
       if (o > 9): 
        addtoerrortable(keyword_result) 
        print('ProvisionedThroughputExceededException 10 times in getSuggestengineResultForThree for allKeywords') 
        break 
       sleep(1) 
       o = o + 1 
       print("ProvisionedThroughputExceededException in getSugestengineResult") 

但我爲每一個線程像這樣的輸出:

Exception in thread Thread-562: 
Traceback (most recent call last): 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 914, in _bootstrap_inner 
self.run() 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 862, in run 
self._target(*self._args, **self._kwargs) 
    File "/Users/iTom/ownCloud/Documents/Workspace/PyCharm/Keywords/TesterWithDB.py", line 362, in getSuggestengineResultForThree 
'keyword': keyword_result 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/boto3/resources/factory.py", line 518, in do_action 
response = action(self, *args, **kwargs) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/boto3/resources/action.py", line 83, in __call__ 
response = getattr(parent.meta.client, operation_name)(**params) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/botocore/client.py", line 252, in _api_call 
return self._make_api_call(operation_name, kwargs) 
    File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/botocore/client.py", line 542, in _make_api_call 
raise ClientError(parsed_response, operation_name) 
botocore.exceptions.ClientError: An error occurred (ProvisionedThroughputExceededException) when calling the PutItem operation: The level of configured provisioned throughput for the table was exceeded. Consider increasing your provisioning level with the UpdateTable API 

有人可以幫助我,讓我自己打印的,而不是追溯? :)

回答

0

有可能ProvisionedThroughputExceededException實際上不是錯誤。試試:

except botocore.exceptions.ClientError as pe: 

改爲。

如果這不起作用,請找出發生錯誤的線路並將except語句放在那裏。

2

這個答案對於你的問題來說有點晚,但這裏是任何人在尋找這個答案。

尼爾回答說boto3拋出的異常是botocore.exceptions.ClientError。但是,您應該檢查'ProvisionedThroughputExceededException'的響應錯誤代碼,因爲ClientError可能是另一個問題。

from botocore.exceptions import ClientError 

except ClientError as e: 
    if e.response['Error']['Code'] != 'ProvisionedThroughputExceededException': 
    raise 
    # do something else with 'e' 

我正在使用Python 2.7,它可能會或可能不會有所作爲。我收到異常表明boto3已經在做重試(最多9次),這是從你的異常不同:

發生錯誤(ProvisionedThroughputExceededException)調用PutItem操作時(達到最大重試次數:9):已超過配置的表的配置吞吐量級別。考慮使用UpdateTable API提高您的配置級別。