From: Richard Purdie <richard.purdie@linuxfoundation.org>
To: openembedded-core <openembedded-core@lists.openembedded.org>
Subject: [PATCH] oe/utils: Add simple threaded pool implementation
Date: Mon, 01 Jun 2015 22:15:34 +0100 [thread overview]
Message-ID: <1433193334.404.178.camel@linuxfoundation.org> (raw)
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>
diff --git a/meta/lib/oe/utils.py b/meta/lib/oe/utils.py
index 6a1a07f..014a9c4 100644
--- a/meta/lib/oe/utils.py
+++ b/meta/lib/oe/utils.py
@@ -209,3 +209,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()
+
reply other threads:[~2015-06-01 21:15 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1433193334.404.178.camel@linuxfoundation.org \
--to=richard.purdie@linuxfoundation.org \
--cc=openembedded-core@lists.openembedded.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox