Python中线程池的实现(三)

n672的头像 n672 17 2015-02-07 22:48 0

 基本信息

× 1   

浏览数: 5753

分享时间: 3 年 前

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


12 [下一页]

  • jhoneila的头像 jhoneila 2018-12-17 15:12 代码数:0

    Nice post mate, keep up the great work, just shared this with my friendz   The Support Local Movement

  • jhoneila的头像 jhoneila 2018-12-26 14:41 代码数:0

    I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.PLTNUM

  • jhoneila的头像 jhoneila 2018-12-30 15:54 代码数:0

    Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work. travel the world

  • jhoneila的头像 jhoneila 2019-01-07 18:49 代码数:0

    I gotta  favorite this website   it seems  very helpful . ukrayna üniversitesi

  • SATEJ1406的头像 SATEJ1406 2019-01-11 21:06 代码数:0

    Hi to everybody, here everyone is sharing such knowledge, so it’s fastidious to see this site, and I used to visit this blog daily Bean<a href="https://games.lol/arcade/">arcade</a>
     

  • jhoneila的头像 jhoneila 2019-01-15 18:48 代码数:0

    I am hoping the same best effort from you in the future as well. In fact your creative writing skills has inspired me. fishing trout in river

  • xgongji的头像 xgongji 2016-08-04 19:59 代码数:0

    写的不错,新手看看

  • jhoneila的头像 jhoneila 2019-01-16 16:55 代码数:0

    This is really a nice and informative, containing all information and also has a great impact on the new technology. Thanks for sharing it pole tent

  • cixexey的头像 cixexey 2018-11-22 21:23 代码数:0

    Friend, this web site might be fabolous, i just like it.travel information

  • cixexey的头像 cixexey 2018-11-24 21:13 代码数:0

    Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article. riverfront residences price

您的评论: