-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadpool.py
executable file
·52 lines (46 loc) · 1.67 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
## {{{ http://code.activestate.com/recipes/577187/ (r9)
from Queue import Queue
import threading
import thread
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:
func, args, kargs = self.tasks.get()
try: func(*args, **kargs)
except Exception, e: print e
self.tasks.task_done()
class ThreadPool:
"""Pool of threads consuming tasks from a queue"""
def __init__(self, num_threads):
self.tasks = Queue(num_threads)
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()
if __name__ == '__main__':
from random import randrange
delays = [randrange(1, 10) for i in range(100)]
from time import sleep
def wait_delay(d):
print threading.currentThread().name, 'sleeping for (%d)sec' % d
sleep(d)
# 1) Init a Thread pool with the desired number of threads
pool = ThreadPool(20)
for i, d in enumerate(delays):
# print the percentage of tasks placed in the queue
print '%.2f%c' % ((float(i)/float(len(delays)))*100.0,'%')
# 2) Add the task to the queue
pool.add_task(wait_delay, d)
# 3) Wait for completion
pool.wait_completion()
## end of http://code.activestate.com/recipes/577187/ }}}