-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtm.py
27 lines (22 loc) · 811 Bytes
/
tm.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
from Queue import Queue
from threading import Thread
class ThreadManager(object):
def _worker(self):
while True:
try:
func, args, kwargs = self._threads.get()
func(*args, **kwargs)
except Exception, e:
print e
self._threads.task_done()
def __init__(self, thread_count):
self._thread_count = thread_count
self._threads = Queue(maxsize=self._thread_count)
for i in range(self._thread_count):
worker_thread = Thread(target=self._worker)
worker_thread.setDaemon(True)
worker_thread.start()
def add_task(self, func, *args, **kwargs):
self._threads.put((func, args, kwargs,))
def wait_for_completion(self):
self._threads.join()