簡體   English   中英

Python:為什么要打印回溯?

[英]Python: Why do is the traceback getting printed?

我有一個功能。 該函數在更多線程中開始使用。 我試圖打印自己的錯誤消息。 但是我仍然要打印回溯的內容並不重要。 我的功能:

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

有人可以幫我自己打印而不是追溯嗎? :)

對於您的問題,這個答案有點晚了,但是如果有人在尋找這個答案,這里是答案。

boto3拋出的異常是botocore.exceptions.ClientError正如Neil回答的那樣。 但是,您應該檢查“ 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次),這與您的異常不同:

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

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

except botocore.exceptions.ClientError as pe:

代替。

如果那行不通,請找出錯誤發生在哪一行,並將except語句放在那里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM