简体   繁体   中英

Python: what did I wrong with Threads or Exceptions?

I have the following function:

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")

The function gets started in more then one thread. I have this process and if the process works, the function should be ready in the thread. Otherwise it should try again 9 times. Now my problem: the "print("ProvisionedThroughputExceededException in getSugestengineResult")" Never got printed. Just the exception as pe gets printed. So there is my problem? Are all the threads working on the same "i"? Or is it never possible to get to the print? I dont know what I am doin wrong ...

You have to use a specific counter if you want all your thread to have the 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):
      with self.lock:
          self.val.value += 1

  def value(self):
      with self.lock:
        return self.val

then you can pass the counter to your function

counter=ThreadCounter(0)
def getSuggestengineResult(suggestengine, seed, tablename,counter):
    ...
    except ProvisionedThroughputExceededException as pe:
            if (counter.value() > 9):
                ...
            counter.increment()
            ...

This counter will be shared with the other threads

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM