when I call join() in a queue, and task_done() has done, but this program still running

时间:2018-02-03 10:24:10

标签: python multithreading queue

I write this code to practice the Queue in Python with thread. when count is 10 or not big integer. it can run correctly, but when count is 1000, this code cannot stop, or sometimes it ended but the queue still not empty. I don't know how to fix it. please help.

import threading
import queue
import random
import time


class Creater(threading.Thread):
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.name = name

    def run(self):
        global count
        while count < 1000:
            print(threading.current_thread().name, 'is running==', count,
                  '==times')
            inputQueue()
            count += 1


class Consumer(threading.Thread):
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.name = name

    def run(self):
        while not q.empty():
            data = outQueue()
            print(threading.current_thread().name, 'get a data:', data)
            print('queue still has', q.qsize())
            q.task_done()


def inputQueue():
    global q
    q.put(random.randint(0, 1000))


def outQueue():
    data = q.get()
    return data


count = 0
q = queue.Queue()
creater = Creater('creater')
consumer = Consumer('consumer')
startFlag = time.time()
creater.start()
consumer.start()
creater.join()
q.join()
spendTime = time.time() - startFlag
print('now Queue size:', q.qsize())
print('creater total run', count, 'times')
print('Finished in', spendTime)

and result sometimes likes this: enter image description here

when i type ctrl + c in the terminal, it shows some error info like this:

  Traceback (most recent call last):
File "xxx.py", line 57, in <module>
  q.join()
File "/home/everglow/anaconda3/lib/python3.6/queue.py", line 83, in join
  self.all_tasks_done.wait()
File "/home/everglow/anaconda3/lib/python3.6/threading.py", line 295, in wait
  waiter.acquire()
KeyboardInterrupt

and now, I think may be it is the task_done() could not call q.join(), so, the main thread always waiting. but I don't know this idea is right or wrong. and I don't know how to fix it.

1 个答案:

答案 0 :(得分:0)

如果消费者设法暂时清空队列,即使创建者仍在运行并且之后在队列中放入更多项目,您的消费者循环while not q.empty():有时会结束。因此,您应该找到一种不同的方式来结束循环,例如计算消耗的项目数量,并在达到1000时结束。