Python thread pool implementation

Source: Internet
Author: User
Code 1 # Coding: UTF-8
2
3 # Python thread pool implementation
4
5 Import Queue
6 Import Threading
7 Import Sys
8 Import Time
9 Import Urllib
10
11 # Threads in the thread pool that work for us
12 Class Mythread (threading. Thread ):
13   Def   _ Init __ (Self, workqueue, resultqueue, timeout = 30 , ** Kwargs ):
14 Threading. thread. _ Init __ (Self, kwargs = Kwargs)
15 # How long does the thread wait for the task queue before the end?
16 Self. Timeout = Timeout
17 Self. setdaemon (true)
18 Self. workqueue = Workqueue
19 Self. resultqueue = Resultqueue
20 Self. Start ()
21
22   Def Run (Self ):
23 While True:
24 Try :
25 # Obtain a task from the work queue
26 Callable, argS, kwargs = Self. workqueue. Get (timeout = Self. Timeout)
27 # Task to be executed
28 Res = Callable (ARGs, kwargs)
29 # The result returned by the reported task is placed in the result queue.
30 Self. resultqueue. Put (Res + " | " + Self. getname ())
31 Except Queue. Empty: # End this thread when the task queue is empty.
32 Break
33 Except :
34 Print SYS. exc_info ()
35 Raise
36
37 Class Threadpool:
38   Def   _ Init __ (Self, num_of_threads = 10 ):
39 Self. workqueue = Queue. Queue ()
40 Self. resultqueue = Queue. Queue ()
41 Self. threads = []
42 Self. _ Createthreadpool (Num_of_threads)
43
44   Def   _ Createthreadpool (Self, num_of_threads ):
45 For I In Range (num_of_threads ):
46 Thread = Mythread (self. workqueue, self. resultqueue)
47 Self. threads. append (thread)
48
49   Def Wait_for_complete (Self ):
50 # Wait until all threads are finished.
51 While Len (self. threads ):
52 Thread = Self. threads. Pop ()
53 # Wait until the thread ends
54 If Thread. isalive (): # Determine whether the thread is still alive to determine whether to call join
55 Thread. Join ()
56
57   Def Add_job (self, callable, * ARGs, ** Kwargs ):
58 Self. workqueue. Put (callable, argS, kwargs ))
59
60 Def Test_job (ID, sleep =   0.001 ):
61 Html =   ""
62   Try :
63 Time. Sleep ( 1 )
64 Conn = Urllib. urlopen ( ' Http://www.google.com/ ' )
65 Html = Conn. Read ( 20 )
66   Except :
67 Print SYS. exc_info ()
68   Return Html
69
70 Def Test ():
71   Print   ' Start testing '
72 TP = Threadpool ( 10 )
73   For I In Range ( 50 ):
74 Time. Sleep ( 0.2 )
75 TP. add_job (test_job, I, I * 0.001 )
76 TP. wait_for_complete ()
77   # Processing result
78   Print   ' Result queue \ ' S length =   % D ' % TP. resultqueue. qsize ()
79   While TP. resultqueue. qsize ():
80 Print TP. resultqueue. Get ()
81   Print   ' End Testing '
82 If   _ Name __   =   ' _ Main __ ' :
83 Test ()

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.