Python中線程池的實現(三)

n672 9年前發布 | 11K 次閱讀 Python

# -- 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!")</pre> 


 本文由用戶 n672 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!