forked from mining/mining
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultithread.py
More file actions
41 lines (33 loc) · 1.11 KB
/
Copy pathmultithread.py
File metadata and controls
41 lines (33 loc) · 1.11 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Queue import Queue
from threading import Thread
from utils import log_it
class Worker(Thread):
"""Thread executing tasks from a given tasks queue"""
def __init__(self, tasks, daemon=False):
Thread.__init__(self)
self.tasks = tasks
self.daemon = daemon
self.start()
def run(self):
func, args, kargs = self.tasks.get()
try:
func(*args, **kargs)
except Exception, e:
log_it(e, 'multithread-worker')
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"""
log_it("ADD TASK", 'multithread-pool')
self.tasks.put((func, args, kargs))
def wait_completion(self):
"""Wait for completion of all the tasks in the queue"""
log_it("COMPLETION", 'multithread-pool')
self.tasks.join()