zoukankan      html  css  js  c++  java
  • python打造线程池

    # coding=utf-8
    import  threading
    import Queue
    import time
    import traceback
    
    class ThreadPoolExecutor(object):
        def __init__(self, max_works):
            self._q = Queue.Queue()                
            self.max_works=max_works
            self.started=0
    
        def worker(self):
            while True:
                (fn, args) = self._q.get()
                try:
                    fn(*args)
                except Exception, e:
                    print '线程池执行错误,item是:',item, '错误原因是:',e,traceback.format_exc()
                finally:
                    pass
                    self._q.task_done()
    
        def submit(self, fn, *args):
            item = (fn,args)
            self._q.put(item)
            self.start_work()
    
    
        def start_work(self):
            if self.started==0:
                for i in range(self.max_works):
                    t=threading.Thread(target=self.worker)
                    t.setDaemon(True)                     ###利用daemon的特性:父线程退出时子线程就自动退出。
                    t.start()
                self.started=1
    
        def wait_all_finish(self):
            self._q.join()

    测试

    #coding=utf8
    import threading
    from xccfb import ThreadPoolExecutor
    import time
    
    if __name__=="__main__":
        tlock=threading.Lock()
        def fun(strx):
            with tlock:
                print time.strftime('%H:%M:%S'),strx
            time.sleep(2)
    
        threadPoolExecutor=ThreadPoolExecutor(3)
        for i in range(10):
            threadPoolExecutor.submit(fun, 'hello')
        threadPoolExecutor.submit(fun, 'hi')
        threadPoolExecutor.wait_all_finish()     ###注释掉就可以先print over
        print time.strftime('%H:%M:%S'), 'over'

    使用wait_all_finish()的queue.join()方法阻塞主线程,当队列中有任务还要执行时候不往下执行。不想阻塞就不要写这句。

  • 相关阅读:
    部分网络加载预训练模型代码实现
    数据不平衡处理方法
    面试题目汇总
    多线程和多进程
    数据结构知识点总结
    GBDT和XGBoost的区别
    GBDT和随机森林的区别
    机器学习-特征处理
    一篇写得很好的关于lct的博客
    Count and Say 2014/10/23
  • 原文地址:https://www.cnblogs.com/ydf0509/p/7636229.html
Copyright © 2011-2022 走看看