python queue thread закрытие треда
from Queue import Queue
from threading import Thread
import time
class Worker(Thread):
"""Thread executing tasks from a given tasks queue"""
def __init__(self, tasks):
Thread.__init__(self)
self.tasks = tasks
self.daemon = True
self.start()
def run(self):
while True:
func, args, kargs = self.tasks.get()
try:
func(*args, **kargs)
except Exception, e:
print e
self.stop()
finally:
self.tasks.task_done()
class ThreadPool:
"""Pool of threads consuming tasks from a queue"""
def __init__(self, num_threads):
self.tasks = Queue(num_threads)
for _ in range(num_threads): Worker(self.tasks)
def add_task(self, func, *args, **kargs):
"""Add a task to the queue"""
self.tasks.put((func, args, kargs))
def wait_completion(self):
"""Wait for completion of all the tasks in the queue"""
# pass
self.tasks.join()
if __name__ == '__main__':
from random import randrange
from time import sleep
delays = [randrange(1, 10) for i in range(100)]
def wait_delay(d):
# print 'sleeping for (%d)sec' % d
sleep(d)
pool = ThreadPool(48)
for i, d in enumerate(delays):
pool.add_task(wait_delay, d)
pool.wait_completion()
while True:
print 'aaa1:', pool
time.sleep(1)
После инструкции pool.wait_completion() хотелось, чтобы все треды умирали. А они не умирают. Как это исправить ?