- # -*- coding: utf-8 -*-
- # Java 理论与实践: 线程池与工作队列: http://www.ibm.com/developerworks/cn/java/j-jtp0730/
- # 线程池原理及python实现: http://www.cnblogs.com/goodhacker/p/3359985.html
- # Threadpool: http://chrisarndt.de/projects/threadpool/
- # http://www.cnblogs.com/coser/archive/2012/03/10/2389264.html
- import Queue
- import threading
- class ThreadPool(object):
- def __init__(self, maxsize=4, timeout=1):
- self._maxsize = maxsize
- self._timeout = timeout
- self._threads = []
- self._work_queue = Queue.Queue()
- self._create_threads()
- def execute(self, func, *args, **kwargs):
- self._work_queue.put((func, args, kwargs))
- # self._append_thread()
- def dismiss(self, do_join=False):
- dismiss_list = []
- for i in range(len(self._threads)):
- thread = self._threads.pop()
- thread.dismiss()
- dismiss_list.append(thread)
- if do_join:
- for thread in dismiss_list:
- thread.join()
- def _create_threads(self):
- for i in range(self._maxsize):
- self._threads.append(WorkThread(self._work_queue, self._timeout))
- # def _append_thread(self):
- # num_thread = len(self._threads)
- # if num_thread == self._maxsize:
- # return
- # num_work = self._work_queue.qsize()
- # if num_thread >= num_work:
- # return
- # for i in range(num_thread, min(num_work, self._maxsize)):
- # self._threads.append(WorkThread(self._work_queue, self._timeout))
- class WorkThread(threading.Thread):
- def __init__(self, work_queue, timeout=1):
- super(WorkThread, self).__init__()
- self._work_queue = work_queue
- self._timeout = timeout
- self._dismissed = threading.Event()
- self.start()
- def run(self):
- while True:
- if self._dismissed.isSet() \\
- and self._work_queue.qsize() == 0:
- break
- try:
- func, args, kwargs = self._work_queue.get(True, self._timeout)
- except Queue.Empty:
- continue
- else:
- func(*args, **kwargs)
- # print("%s exited!" % threading.current_thread())
- def dismiss(self):
- self._dismissed.set()
- if __name__ == '__main__':
- import time
- def do_sth(n):
- time.sleep(0.1)
- print("task%s in %s" % (n, threading.current_thread()))
- pool = ThreadPool()
- for i in range(0, 20):
- pool.execute(do_sth, i)
- pool.dismiss(True)
- print("completed!")
- #该片段来自于http://www.codesnippet.cn/detail/120520149527.html
来源: http://www.codesnippet.cn/detail/120520149527.html