forked from PanDAWMS/pilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadPool.py
71 lines (56 loc) · 1.89 KB
/
ThreadPool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from Queue import Queue
from threading import Thread
class Worker(Thread):
"""Thread executing tasks from a given tasks queue"""
def __init__(self, tasks):
Thread.__init__(self)
self.tasks = tasks
self.daemon = True
self.start()
def run(self):
while True:
try:
func, args, kargs = self.tasks.get()
try:
func(*args, **kargs)
except Exception, e:
print "ThreadPool Worker exception: %s" % str(e)
except:
print "ThreadPool Worker unknow exception."
except:
print "ThreadPool Worker unknow exception1"
finally:
self.tasks.task_done()
class ThreadPool:
"""Pool of threads consuming tasks from a queue"""
def __init__(self, num_threads):
self.tasks = Queue()
for _ in range(num_threads): Worker(self.tasks)
def add_task(self, func, *args, **kargs):
"""Add a task to the queue"""
self.tasks.put((func, args, kargs))
def wait_completion(self):
"""Wait for completion of all the tasks in the queue"""
self.tasks.join()
def is_empty(self):
return self.tasks.empty()
if __name__ == '__main__':
from random import randrange
from time import sleep
delays = [randrange(1, 10) for i in range(100)]
def wait_delay(d):
print 'sleeping for (%d)sec' % d
sleep(d)
pool = ThreadPool(20)
for i, d in enumerate(delays):
pool.add_task(wait_delay, d)
sleep(100)
delays = [randrange(10, 20) for i in range(100)]
for i, d in enumerate(delays):
pool.add_task(wait_delay, d)
print 'wait completion'
pool.wait_completion()
for i, d in enumerate(delays):
pool.add_task(wait_delay, d)
print 'wait completion'
pool.wait_completion()