-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathThreadPool.py
33 lines (31 loc) · 926 Bytes
/
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
import threading
from Queue import Queue
#Define a thread class to parse the avro file (filename)
#Thread executes tasks from a given tasks queue
class Worker(threading.Thread):
def __init__(self, tasks):
threading.Thread.__init__(self)
#tasks is the shared task queue
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
finally:
self.tasks.task_done()
#ThreadPool consuming tasks form a queue
class ThreadPool:
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()