AttributeError:'队列'对象没有属性' task_done'

时间:2017-06-26 20:56:08

标签: python multithreading python-3.x multiprocessing python-multithreading

任何人都可以解释为什么我会收到以下错误吗?

我正在使用python 3.4

Exception in thread Thread-5:
Traceback (most recent call last):
  File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner
    self.run()
  File "/home/arron/Downloads/Snomed/worker/neo4j_upload_worker.py", line 36, in run
    self.queue.task_done()
AttributeError: 'Queue' object has no attribute 'task_done'

from queue import Queue
from threading import Thread
import time


class Neo4jUploadWorker(Thread):
num_worker_threads = 8
value = 0

def __init__(self, queue, item_processor):
    Thread.__init__(self)
    self.queue = queue
    self.tx = None
    self.idx = 0
    self.item_processor = item_processor

def run(self):
    try:
        while True:
            # Get the work from the queue and expand the tuple
            item = self.queue.get()
            if item is None:
                break
            if self.idx % 1000 == 0 and self.idx != 0:
                if self.tx is not None:
                    time.sleep(0.2)
                    self.tx.commit()
                self.tx = self.item_processor.graph.cypher.begin()
                print('committed 1000 rows till row:' + str(self.idx))
            if self.idx == 0:
                self.tx = self.item_processor.graph.cypher.begin()
            self.idx += 1
            self.item_processor.process(item, self.tx)
            self.queue.task_done()
    finally:
        print('in the worker finally')
        if self.tx is not None and not self.tx.finished:
            self.tx.commit()

根据python 3文档,我已经将它称为应该如何:

def worker():
    while True:
        item = q.get()
        if item is None:
            break
        do_work(item)
        q.task_done() 

明显使用自我。

提前感谢。

1 个答案:

答案 0 :(得分:0)

此问题已通过确保使用正确版本的Python来解决。