加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python中线程池的实现(三)

发布时间:2020-12-17 17:25:13 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 # -*- coding: utf-8 -*-# Java 理论与实践: 线程池与工作队列: http://www.ibm.com/developerworks/cn/java/j-jtp0730/# 线程池原理及python实现: ht

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读