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-01 22:18:14 +0100 |
commit | 44ae778fefca5112900b870be7a485360c50bc2e (patch) | |
tree | 481126a40c5362ff6f7422a93b294845f2404f82 /meta | |
parent | dee005b6e1bc353230f9f27a469b2054a644e542 (diff) | |
download | openembedded-core-44ae778fefca5112900b870be7a485360c50bc2e.tar.gz openembedded-core-44ae778fefca5112900b870be7a485360c50bc2e.tar.bz2 openembedded-core-44ae778fefca5112900b870be7a485360c50bc2e.zip |
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.
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta')
-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): def squashspaces(string): import re return re.sub("\s+", " ", string).strip() + +# +# Python 2.7 doesn't have threaded pools (just multiprocessing) +# so implement a version here +# + +from Queue import Queue +from threading import Thread + +class ThreadedWorker(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, e: + print e + finally: + self.tasks.task_done() + +class ThreadedPool: + """Pool of threads consuming tasks from a queue""" + def __init__(self, num_threads): + self.tasks = Queue(num_threads) + for _ in range(num_threads): ThreadedWorker(self.tasks) + + def add_task(self, func, *args, **kargs): + """Add a task to the queue""" + self.tasks.put((func, args, kargs)) + + def wait_completion(self): + """Wait for completion of all the tasks in the queue""" + self.tasks.join() + |