OLD | NEW |
1 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import cgi | 5 import cgi |
6 import ConfigParser | 6 import ConfigParser |
7 import json | 7 import json |
8 import os | 8 import os |
| 9 import Queue |
| 10 import threading |
9 import time | 11 import time |
10 | 12 |
11 from common import utils | 13 from common import utils |
12 from result import Result | 14 from result import Result |
13 | 15 |
14 | 16 |
15 INFINITY = float('inf') | 17 INFINITY = float('inf') |
16 | 18 |
| 19 MAX_THREAD_NUMBER = 10 |
| 20 TASK_QUEUE = None |
| 21 |
| 22 |
| 23 def Worker(): |
| 24 global TASK_QUEUE |
| 25 while True: |
| 26 function, args, kwargs, result_semaphore = TASK_QUEUE.get() |
| 27 try: |
| 28 function(*args, **kwargs) |
| 29 except: |
| 30 pass |
| 31 finally: |
| 32 # Signal one task is done in case of exception. |
| 33 result_semaphore.release() |
| 34 |
| 35 |
| 36 def RunTasks(tasks): |
| 37 """Run given tasks. Not thread-safe: no concurrent calls of this function. |
| 38 |
| 39 Return after all tasks were completed. A task is a dict as below: |
| 40 { |
| 41 'function': the function to call, |
| 42 'args': the positional argument to pass to the function, |
| 43 'kwargs': the key-value arguments to pass to the function, |
| 44 } |
| 45 """ |
| 46 if not tasks: |
| 47 return |
| 48 |
| 49 global TASK_QUEUE |
| 50 if not TASK_QUEUE: |
| 51 TASK_QUEUE = Queue.Queue() |
| 52 for index in range(MAX_THREAD_NUMBER): |
| 53 thread = threading.Thread(target=Worker, name='worker_%s' % index) |
| 54 # Set as daemon, so no join is needed. |
| 55 thread.daemon = True |
| 56 thread.start() |
| 57 |
| 58 result_semaphore = threading.Semaphore(0) |
| 59 # Push task to task queue for execution. |
| 60 for task in tasks: |
| 61 TASK_QUEUE.put( |
| 62 (task['function'], task.get('args', []), |
| 63 task.get('kwargs', {}), result_semaphore)) |
| 64 |
| 65 # Wait until all tasks to be executed. |
| 66 for _ in tasks: |
| 67 result_semaphore.acquire() |
| 68 |
17 | 69 |
18 def GetRepositoryType(revision_number): | 70 def GetRepositoryType(revision_number): |
19 """Returns the repository type of this revision number. | 71 """Returns the repository type of this revision number. |
20 | 72 |
21 Args: | 73 Args: |
22 revision_number: A revision number or git hash. | 74 revision_number: A revision number or git hash. |
23 | 75 |
24 Returns: | 76 Returns: |
25 'git' or 'svn', depending on the revision_number. | 77 'git' or 'svn', depending on the revision_number. |
26 """ | 78 """ |
(...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 review_url = None | 554 review_url = None |
503 reviewers = None | 555 reviewers = None |
504 line_content = blame.line_content | 556 line_content = blame.line_content |
505 message = blame.message | 557 message = blame.message |
506 | 558 |
507 result = Result(suspected_cl, revision_url, component_name, author, reason, | 559 result = Result(suspected_cl, revision_url, component_name, author, reason, |
508 review_url, reviewers, line_content, message) | 560 review_url, reviewers, line_content, message) |
509 result_list.append(result) | 561 result_list.append(result) |
510 | 562 |
511 return result_list | 563 return result_list |
OLD | NEW |