Python Multithreading and thread pool

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.