标签:
线程池:线程池主要用来解决线程生命周期开销问题和资源不足问题。通过对多个任务重用线程,线程创建的开销就被分摊到了多个任务上了,线程池中的所有线程主动从工作队列中寻找执行的工作。
import sys, Queue, threading import time class _Thread(threading.Thread): def __init__(self, workQueue, resultQueue,timeout=1, **kwargs): threading.Thread.__init__(self, kwargs=kwargs) self.timeout = timeout self.setDaemon(True) self.workQueue = workQueue self.resultQueue = resultQueue # self.start() def run(self): while True: try: callable, args, kwargs = self.workQueue.get(timeout=self.timeout) res = callable(args, kwargs) print res," | "+self.getName() self.resultQueue.put(res+" | "+self.getName()) except Queue.Empty: break except : print sys.exc_info() raise class ThreadPool: def __init__( self, num_of_threads=2): self.workQueue = Queue.Queue() #work queue func args self.resultQueue = Queue.Queue() #resulst queue self.threads = [] #threadlist self.__createThreadPool(num_of_threads) def __createThreadPool( self, num_of_threads ): for i in range( num_of_threads ): thread = _Thread( self.workQueue, self.resultQueue ) self.threads.append(thread) def wait_for_complete(self): while len(self.threads): thread = self.threads.pop() if thread.isAlive(): thread.join() def start(self): for th in self.threads: th.start() def add_job( self, callable, *args, **kwargs ): self.workQueue.put( (callable,args,kwargs) ) #put msg def test_job(id, sleep = 0.001 ): time.sleep(0.1) return str(id) def test(): print ‘start testing‘ tp = ThreadPool(5) for i in range(50): tp.add_job( test_job, i, i ) tp.start() tp.wait_for_complete() print ‘result Queue\‘s length == %d ‘% tp.resultQueue.qsize() while tp.resultQueue.qsize(): print tp.resultQueue.get() print ‘end testing‘ if __name__ == ‘__main__‘: test()
标签:
原文地址:http://www.cnblogs.com/pylab/p/4575290.html