Python线程池实现

线程池:
#-*- coding: gb2312 -*-
'''
Created on Apr 10, 2014
@author: liYunFeng
@see: 线程池模块
'''
import Queue
import sys   
import threading
from time import ctime

class ThreadPool(threading.Thread):     
    
    def __init__(self, workQueue, resultQueue, timeout = 0.5, **kwds):
        threading.Thread.__init__(self, **kwds)      
        self.setDaemon(True) #创建该线程的主线程结束时,Kill子线程  
        self.workQueue=workQueue #工作队列
        self.resultQueue=resultQueue #结果队列
        self.timeout=timeout

    def run(self):
        while True:
            try:    
                callable, args, kwds=self.workQueue.get(timeout = self.timeout)
                print ctime(),self.getName(),'the task number of:',args[0]
                res=callable(*args, **kwds) 
                self.resultQueue.put(res)#将结果放入到结果的队列里面   
            except Queue.Empty:#如果去工作队列取函数和参数的时候,队列为空的话,跳出结束循环
                break    
            except:
                print ctime(),self.getName(), sys.exc_info()[:2]    
  
'''线程池管理'''   
class ThreadPoolManager(object):
    
    __instance = None
    __lock = threading.RLock()
    
    def __init__(self):
        pass
    
    '''单例'''
    @classmethod
    def getInstance(self):
        #加锁
        self.__lock.acquire()
        if not self.__instance:
            self.__instance = super(ThreadPoolManager,self).__new__(self)
        self.__lock.release()
        
        return self.__instance
    
    '''初始化线程池'''
    def initPool(self, num_of_workers = 5, timeout = 0.5):
        self.workQueue=Queue.Queue()#创建工作队列
        self.resultQueue=Queue.Queue()#创建结果队列     
        self.workers=[]#线程容器
        self.timeout=timeout
        
        for i in range(num_of_workers):    
            worker=ThreadPool(self.workQueue, self.resultQueue, self.timeout)#线程就绪    
            worker.setName("Task-"+str(i+1))#给每个线程设置固定的名字
            self.workers.append(worker)#将线程加入到一个列表中
        
        self.start()
            
    '''开启线程'''
    def start(self):
        for t in self.workers:
            t.start()
            
        print ctime(),'All task is start,of ',len(self.workers)
    
    '''等待所有线程完成'''
    def wait_complete(self):    
        ''' ...then, wait for each of them to terminate'''
        while len(self.workers):
            worker=self.workers.pop()#取出一个工作线程     
            worker.join()#阻塞在此,线程结束后才往后走。这里并没有启动线程,线程一被创建就已经启动了
            if worker.isAlive() and not self.workQueue.empty():#isAlive方法查看线程是否运行    
                self.workers.append(worker)
            
        print ctime(),"All task is complete!"
        print ''
    
    '''加入工作队列,第二个参数是函数,第三个参数是函数的参数'''  
    def add_Task(self, callable, *args, **kwds):    
        self.workQueue.put((callable, args, kwds))#必须按元组的方式添加进去
    
    '''通过参数来获得从队列中获得特定的结果'''
    def get_result(self, *args, **kwds):    
        return self.resultQueue.get(*args, **kwds)

    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值