Python中线程池的实现(三)

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
# -*- 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!")

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:Android开发之蓝牙操作实例

下一篇:Android手机截屏代码