1Code highlighting produced by Actipro Codehighlighter (freeware) http://www.codehighlighter.com/--> 1#Coding:utf-82 3 #the thread pool implementation of Python4 5 ImportQueue6 ImportThreading7 ImportSYS8 Import Time9 ImportUrllibTen One #threads in the thread pool that work for us A classMyThread (Threading. Thread): - def __init__(Self, workQueue, resultqueue,timeout=30, * *Kwargs): -Threading. Thread.__init__(Self, kwargs=Kwargs) the #How long the thread waits for the task queue before it ends -Self.timeout =Timeout - Self.setdaemon (True) -Self.workqueue =WorkQueue +Self.resultqueue =Resultqueue - Self.start () + A defRun (self): at whileTrue: - Try: - #get a task from the work queue -Callable, args, Kwargs = Self.workQueue.get (timeout=self.timeout) - #The task we're going to perform -res =callable (args, Kwargs) in #report task returns results in the result queue -Self.resultQueue.put (res+" | "+self.getname ()) to exceptQueue.empty:#end this thread when the task queue is empty + Break - except : the PrintSys.exc_info () * Raise $ Panax Notoginseng classThreadPool: - def __init__(Self, num_of_threads=10): theSelf.workqueue =Queue.queue () +Self.resultqueue =Queue.queue () ASelf.threads = [] theSelf.__createthreadpool(num_of_threads) + - def __createthreadpool(self, num_of_threads): $ forIinchRange (num_of_threads): $Thread =MyThread (Self.workqueue, Self.resultqueue) - self.threads.append (thread) - the defWait_for_complete (self): - #wait for all threads to complete. Wuyi whileLen (self.threads): theThread =Self.threads.pop () - #wait for thread to end Wu ifThread.isalive ():#determines whether a thread is still alive to decide whether to call join - Thread.Join () About $ defAdd_job (self, callable, *args, * *Kwargs): - Self.workQueue.put ((Callable,args,kwargs)) - - defTest_job (id, sleep = 0.001 ): AHTML ="" + Try: theTime.sleep (1) -conn = Urllib.urlopen ('http://www.google.com/') $html = Conn.read (20) the except: the PrintSys.exc_info () the returnHTML the - defTest (): in Print 'Start Testing' theTP = ThreadPool (10) the forIinchRange (50): AboutTime.sleep (0.2) theTp.add_job (Test_job, I, i*0.001 ) the Tp.wait_for_complete () the #Processing Results + Print 'result queue\ ' s length = =%d'%tp.resultQueue.qsize () - whiletp.resultQueue.qsize (): the PrintTp.resultQueue.get ()Bayi Print 'End Testing' the if __name__=='__main__': theTest ()
Python Multithreading and thread pool