zoukankan      html  css  js  c++  java
  • 绝版线程池

    import threading,queue,time
    
    StopEvent = object()
    
    class Mythreadpool:
    
        def __init__(self,max_num):
            self.q = queue.Queue()
            self.max_num = max_num
            self.generate_list = []
            self.free_list = []
    
        def run(self,func,args,callback=None):
            w = func,args,callback
            self.q.put(w)
            if len(self.free_list) == 0 and len(self.generate_list) < self.max_num:
                self.generate_thread()
    
        def generate_thread(self):
            t = threading.Thread(target=self.call)
            t.start()
    
        def call(self):
            current_thread = threading.currentThread
            self.generate_list.append(current_thread)
            event = self.q.get()
    
            while event != StopEvent:
                func,args,callback = event
                try:
                    ret = func(args)
                except Exception as e:
                        ret = e
                try:
                    callback(ret)
                except Exception as b:
                    pass
    
                self.free_list.append(current_thread)
                event = self.q.get()
                self.free_list.remove(current_thread)
    
            else:
    
                self.generate_list.remove(current_thread)
    
        def close(self):
            mun = len(self.generate_list)
            while mun:
                self.q.put(StopEvent)
                mun -= 1
    
    
    
    def fun(i):
        time.sleep(0.5)
        print(i)
    
    pool = Mythreadpool(15)
    
    for i in range(100):
        pool.run(func=fun,args=i,)
    
    pool.close()
    

      

    #with 版本
    import threading,queue,time,contextlib
    
    StopEvent = object()
    
    class Mythreadpool:
    
        def __init__(self,max_num):
            self.q = queue.Queue()
            self.max_num = max_num
            self.generate_list = []
            self.free_list = []
    
        def run(self,func,args,callback=None):
            w = func,args,callback
            self.q.put(w)
            if len(self.free_list) == 0 and len(self.generate_list) < self.max_num:
                self.generate_thread()
    
        def generate_thread(self):
            t = threading.Thread(target=self.call)
            t.start()
    
        def call(self):
            current_thread = threading.currentThread
            self.generate_list.append(current_thread)
            event = self.q.get()
    
            while event != StopEvent:
                func,args,callback = event
                try:
                    ret = func(args)
                except Exception as e:
                        ret = e
                try:
                    callback(ret)
                except Exception as b:
                    pass
                with self.with_func(self.free_list,current_thread):
                # self.free_list.append(current_thread)
                    event = self.q.get()
                # self.free_list.remove(current_thread)
    
            else:
    
                self.generate_list.remove(current_thread)
                
        @contextlib.contextmanager
        def with_func(self,list_1,args):
            list_1.append(args)
            try:
                yield
            finally:
                list_1.remove(args)
    
    
        def close(self):
            mun = len(self.generate_list)
            while mun:
                self.q.put(StopEvent)
                mun -= 1
    
    
    
    def fun(i):
        time.sleep(0.5)
        print(i)
    
    pool = Mythreadpool(15)
    
    for i in range(100):
        pool.run(func=fun,args=i,)
    
    pool.close()
    

      

  • 相关阅读:
    我的第一个网络爬虫
    python中os模块的walk函数
    <Think Python>中统计文献单词的处理代码
    <Think Python>中斐波那契使用memo实现的章节
    Python中bisect的使用
    vim中使用系统粘贴板
    linux下的重命名
    HTML Dog 初级教程中关于 forms 的翻译
    unittest:1 用例编写
    python UI自动化实战记录十一: 总结
  • 原文地址:https://www.cnblogs.com/cloniu/p/6287008.html
Copyright © 2011-2022 走看看