/
threadpool.py
49 lines (38 loc) · 1.23 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
'''
file: threadpool.py
description: Inspired by http://stackoverflow.com/a/7257510
'''
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:
func, args, kargs = self.tasks.get()
try:
func(*args, **kargs)
except Exception as e:
print(e)
finally:
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 map(self, func, args_list):
''' Add a list of tasks to the queue '''
for args in args_list:
self.add_task(func, args)
def wait_completion(self):
''' Wait for completion of all the tasks in the queue '''
self.tasks.join()