multithreading - Python: what did I wrong with Threads or Exceptions? -
i have following function:
def getsuggestengineresult(suggestengine, seed, tablename): table = gettable(tablename) keyword_result in results[seed][suggestengine]: = 0 while true: try: allkeywords.put_item( item={ 'keyword': keyword_result } ) break except provisionedthroughputexceededexception pe: if (i > 9): addtoerrortable(keyword_result) print(pe) break sleep(1) = + 1 print("provisionedthroughputexceededexception in getsugestengineresult")
the function gets started in more 1 thread. have process , if process works, function should ready in thread. otherwise should try again 9 times. problem: "print("provisionedthroughputexceededexception in getsugestengineresult")" never got printed. exception pe gets printed. there problem? threads working on same "i"? or never possible print? dont know doin wrong ...
you have use specific counter if want thread have same counter :
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): self.lock: self.val.value += 1 def value(self): self.lock: return self.val
then can pass counter function
counter=threadcounter(0) def getsuggestengineresult(suggestengine, seed, tablename,counter): ... except provisionedthroughputexceededexception pe: if (counter.value() > 9): ... counter.increment() ...
this counter shared other threads
Comments
Post a Comment