线程池:
#-*- 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)