diff options
Diffstat (limited to 'meta/lib/oe/utils.py')
-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 | |||