为什么在从python Queue中消耗掉所有工作后才释放线程

时间:2016-02-26 08:17:42

标签: python multithreading

我使用Queue来提供线程可以处理的任务。从Queue完成所有工作后,我发现线程仍处于活动状态,而我预计它们会被释放。这是我的代码。在一批任务(在同一队列中)从控制台增加后,您可以看到活动线程数正在增加。在一批工作完成后我怎么能释放线程?

import threading
import time
from Queue import Queue

class ThreadWorker(threading.Thread):
    def __init__(self, task_queue):
        threading.Thread.__init__(self)
        self.task_queue = task_queue

    def run(self):
        while True:
            work = self.task_queue.get()
            #do some work
            # do_work(work)
            time.sleep(0.1)
            self.task_queue.task_done()

def get_batch_work_done(works):
    task_queue =  Queue()
    for _ in range(5):
        t = ThreadWorker(task_queue)
        t.setDaemon(True)
        t.start()
    for work in range(works):
       task_queue.put(work)
    task_queue.join()
    print 'get batch work done'
    print 'active threads count is {}'.format(threading.activeCount())

if __name__ == '__main__':
    for work_number in range(3):
        print 'start with {}'.format(work_number)
        get_batch_work_done(work_number)

enter image description here

1 个答案:

答案 0 :(得分:1)

在循环中执行非阻塞读取并使用异常处理来终止

def run(self):
      try:            
        while True:            
          work = self.task_queue.get(True, 0.1)
        #do some work
        # do_work(work)
      except Queue.Empty:
        print "goodbye"