[英]Python: what did I wrong with Threads or Exceptions?
我有以下功能:
def getSuggestengineResult(suggestengine, seed, tablename):
table = getTable(tablename)
for keyword_result in results[seed][suggestengine]:
i = 0
while True:
try:
allKeywords.put_item(
Item={
'keyword': keyword_result
}
)
break
except ProvisionedThroughputExceededException as pe:
if (i > 9):
addtoerrortable(keyword_result)
print(pe)
break
sleep(1)
i = i + 1
print("ProvisionedThroughputExceededException in getSugestengineResult")
該函數在多個線程中啟動。 我有這個過程,如果過程有效,該函數應該在線程中准備就緒。 否則它應該再試一次9次。 現在我的問題:“打印(”getSugestengineResult中的ProvisionedThroughputExceededException“)”從未打印過。 只是pe被打印的例外。 所以有我的問題? 所有線程都在同一個“i”上工作嗎? 或者它永遠不可能進入印刷品? 我不知道我做錯了什么...
如果您希望所有線程都具有相同的計數器,則必須使用特定的計數器:
from multiprocessing import Lock, Process, Value
class ThreadCounter(object):
def __init__(self, initval=0):
self.val = Value('i', initval)
self.lock = Lock()
def increment(self):
with self.lock:
self.val.value += 1
def value(self):
with self.lock:
return self.val
然后你可以將計數器傳遞給你的功能
counter=ThreadCounter(0)
def getSuggestengineResult(suggestengine, seed, tablename,counter):
...
except ProvisionedThroughputExceededException as pe:
if (counter.value() > 9):
...
counter.increment()
...
此計數器將與其他線程共享
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.