diff options
author | Richard Purdie <richard.purdie@linuxfoundation.org> | 2015-06-01 22:15:34 +0100 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2015-06-03 16:38:45 +0100 |
commit | 5331bbc1ac797e7503dd7fcf9780b0eab7274fc7 (patch) | |
tree | 937371ab103d3003b6d3cdc1957b71287fea0c4f /meta/lib | |
parent | 9fd3c7e64b55bc2d984aa7fdac2f0e2ed58b9033 (diff) | |
download | poky-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.py | 41 |
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): | |||
207 | def squashspaces(string): | 207 | def 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 | |||
216 | from Queue import Queue | ||
217 | from threading import Thread | ||
218 | |||
219 | class 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 | |||
237 | class 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 | |||