summaryrefslogtreecommitdiffstats
path: root/meta/lib
diff options
context:
space:
mode:
authorRichard Purdie <richard.purdie@linuxfoundation.org>2015-06-01 22:15:34 +0100
committerRichard Purdie <richard.purdie@linuxfoundation.org>2015-06-03 16:38:45 +0100
commit5331bbc1ac797e7503dd7fcf9780b0eab7274fc7 (patch)
tree937371ab103d3003b6d3cdc1957b71287fea0c4f /meta/lib
parent9fd3c7e64b55bc2d984aa7fdac2f0e2ed58b9033 (diff)
downloadpoky-5331bbc1ac797e7503dd7fcf9780b0eab7274fc7.tar.gz
oe/utils: Add simple threaded pool implementation
Python 2.7 doesn't have a threaded pool implementation, just a multiprocessing one. We have need of a threaded implementation so add some simple class code to support this. (From OE-Core rev: 44ae778fefca5112900b870be7a485360c50bc2e) Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/lib')
-rw-r--r--meta/lib/oe/utils.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/meta/lib/oe/utils.py b/meta/lib/oe/utils.py
index 7173e106f5..0de880013a 100644
--- a/meta/lib/oe/utils.py
+++ b/meta/lib/oe/utils.py
@@ -207,3 +207,44 @@ def multiprocess_exec(commands, function):
207def squashspaces(string): 207def squashspaces(string):
208 import re 208 import re
209 return re.sub("\s+", " ", string).strip() 209 return re.sub("\s+", " ", string).strip()
210
211#
212# Python 2.7 doesn't have threaded pools (just multiprocessing)
213# so implement a version here
214#
215
216from Queue import Queue
217from threading import Thread
218
219class ThreadedWorker(Thread):
220 """Thread executing tasks from a given tasks queue"""
221 def __init__(self, tasks):
222 Thread.__init__(self)
223 self.tasks = tasks
224 self.daemon = True
225 self.start()
226
227 def run(self):
228 while True:
229 func, args, kargs = self.tasks.get()
230 try:
231 func(*args, **kargs)
232 except Exception, e:
233 print e
234 finally:
235 self.tasks.task_done()
236
237class ThreadedPool:
238 """Pool of threads consuming tasks from a queue"""
239 def __init__(self, num_threads):
240 self.tasks = Queue(num_threads)
241 for _ in range(num_threads): ThreadedWorker(self.tasks)
242
243 def add_task(self, func, *args, **kargs):
244 """Add a task to the queue"""
245 self.tasks.put((func, args, kargs))
246
247 def wait_completion(self):
248 """Wait for completion of all the tasks in the queue"""
249 self.tasks.join()
250