Index: tools/findit/crash_utils.py |
diff --git a/tools/findit/crash_utils.py b/tools/findit/crash_utils.py |
index 4aa5fbdfd7828a381529249e80b084e7b18c3e17..6be058b0823fa8f635b13090e169362a875d7b79 100644 |
--- a/tools/findit/crash_utils.py |
+++ b/tools/findit/crash_utils.py |
@@ -6,6 +6,8 @@ import cgi |
import ConfigParser |
import json |
import os |
+import Queue |
+import threading |
import time |
from common import utils |
@@ -14,6 +16,56 @@ from result import Result |
INFINITY = float('inf') |
+MAX_THREAD_NUMBER = 10 |
+TASK_QUEUE = None |
+ |
+ |
+def Worker(): |
+ global TASK_QUEUE |
+ while True: |
+ function, args, kwargs, result_semaphore = TASK_QUEUE.get() |
+ try: |
+ function(*args, **kwargs) |
+ except: |
+ pass |
+ finally: |
+ # Signal one task is done in case of exception. |
+ result_semaphore.release() |
+ |
+ |
+def RunTasks(tasks): |
+ """Run given tasks. Not thread-safe: no concurrent calls of this function. |
+ |
+ Return after all tasks were completed. A task is a dict as below: |
+ { |
+ 'function': the function to call, |
+ 'args': the positional argument to pass to the function, |
+ 'kwargs': the key-value arguments to pass to the function, |
+ } |
+ """ |
+ if not tasks: |
+ return |
+ |
+ global TASK_QUEUE |
+ if not TASK_QUEUE: |
+ TASK_QUEUE = Queue.Queue() |
+ for index in range(MAX_THREAD_NUMBER): |
+ thread = threading.Thread(target=Worker, name='worker_%s' % index) |
+ # Set as daemon, so no join is needed. |
+ thread.daemon = True |
+ thread.start() |
+ |
+ result_semaphore = threading.Semaphore(0) |
+ # Push task to task queue for execution. |
+ for task in tasks: |
+ TASK_QUEUE.put( |
+ (task['function'], task.get('args', []), |
+ task.get('kwargs', {}), result_semaphore)) |
+ |
+ # Wait until all tasks to be executed. |
+ for _ in tasks: |
+ result_semaphore.acquire() |
+ |
def GetRepositoryType(revision_number): |
"""Returns the repository type of this revision number. |