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


× 1   

浏览数: 5753

分享时间: 3 年 前

# -*- coding: utf-8 -*-

# Java 理论与实践: 线程池与工作队列:
# 线程池原理及python实现:
# Threadpool:

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()
    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()
        if do_join:
            for thread in dismiss_list:
    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()
    def run(self):
        while True:
            if self._dismissed.isSet() \
                    and self._work_queue.qsize() == 0:
                func, args, kwargs = self._work_queue.get(True, self._timeout)
            except Queue.Empty:
                func(*args, **kwargs)
        # print("%s exited!" % threading.current_thread())
    def dismiss(self):

if __name__ == '__main__':
    import time

    def do_sth(n):
        print("task%s in %s" % (n, threading.current_thread()))

    pool = ThreadPool()
    for i in range(0, 20):
        pool.execute(do_sth, i)


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="">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 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