错误:queue.task_done()在Python中调用了很多次

时间:2017-06-26 09:33:36

标签: python multithreading queue

我现在被困在这个

class ThreadingPower(threading.Thread):
def __init__(self, queue):
    threading.Thread.__init__(self)
    self.queue= queue

def run(self):
    while True:
        Manager = self.queue.get()
        for Numbers,HWID in Manager:
            r = requests.post(URL, data=payload) # License Checker Required Numbers ( Buyer Code ) And HWID ( License Code )
            data = (r.text)
            if ('Verified' in data):
                with open(resultsFile,'a+') as results: 
                    results.write("The Number : "+str(Numbers)+" Is Verified By The "+str(HWID))
                results.close()
                print str(HWID)+" Is Server Verified"
                active =+ 1
                self.queue.task_done()
            else:
                print str(HWID)+" Is Not Server Verified"
                self.queue.task_done()


if __name__ == "__main__":
    for i in range(10):
        t = ThreadingPower(queue)
        t.setDaemon(False)
        t.start()

    queue.put(credentials)
    queue.join()

证书就像这样:

[
 ['UniqueHWID', 'BuyerCode'], 
 ['UniqueHWID', 'BuyerCode'], 
 ['UniqueHWID', 'BuyerCode']
]

它保持了多次呼叫错误

1 个答案:

答案 0 :(得分:0)

  

问题:错误:queue.task_done()调用多次

.task_done()的来电应与.queue.get()同步 您在.task_done()循环内多次执行for ...

将您的代码更改为:

while True:
    Manager = self.queue.get()
    for Numbers,HWID in Manager:
        ...
        if ('Verified' in data):
            ...
            active =+ 1
        else:
            print str(HWID)+" Is Not Server Verified"

    self.queue.task_done()