| OLD | NEW |
| 1 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 1 # Copyright (c) 2011 The Chromium OS 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 """Module containing methods/classes related to running parallel test jobs.""" | 5 """Module containing methods/classes related to running parallel test jobs.""" |
| 6 | 6 |
| 7 import multiprocessing | 7 import multiprocessing |
| 8 import sys | 8 import sys |
| 9 import time | 9 import time |
| 10 | 10 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 number_of_simultaneous_jobs: Max number of parallel_jobs to be run in | 97 number_of_simultaneous_jobs: Max number of parallel_jobs to be run in |
| 98 parallel. | 98 parallel. |
| 99 jobs: Array of methods to run. | 99 jobs: Array of methods to run. |
| 100 jobs_args: Array of args associated with method calls. | 100 jobs_args: Array of args associated with method calls. |
| 101 print_status: True if you'd like this to print out .'s as it runs jobs. | 101 print_status: True if you'd like this to print out .'s as it runs jobs. |
| 102 Returns: | 102 Returns: |
| 103 Returns an array of results corresponding to each parallel_job. | 103 Returns an array of results corresponding to each parallel_job. |
| 104 """ | 104 """ |
| 105 def ProcessOutputWrapper(func, args, output): | 105 def ProcessOutputWrapper(func, args, output): |
| 106 """Simple function wrapper that puts the output of a function in a queue.""" | 106 """Simple function wrapper that puts the output of a function in a queue.""" |
| 107 output.put(func(*args)) | 107 try: |
| 108 output.put(func(*args)) |
| 109 except: |
| 110 output.put('') |
| 111 raise |
| 112 finally: |
| 113 output.close() |
| 108 | 114 |
| 109 assert len(jobs) == len(jobs_args), 'Length of args array is wrong.' | 115 assert len(jobs) == len(jobs_args), 'Length of args array is wrong.' |
| 110 # Cache sudo access. | 116 # Cache sudo access. |
| 111 cros_lib.RunCommand(['sudo', 'echo', 'Caching sudo credentials'], | 117 cros_lib.RunCommand(['sudo', 'echo', 'Caching sudo credentials'], |
| 112 print_cmd=False, redirect_stdout=True, | 118 print_cmd=False, redirect_stdout=True, |
| 113 redirect_stderr=True) | 119 redirect_stderr=True) |
| 114 | 120 |
| 115 parallel_jobs = [] | 121 parallel_jobs = [] |
| 116 output_array = [] | 122 output_array = [] |
| 117 | 123 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 128 output_array.append(output) | 134 output_array.append(output) |
| 129 | 135 |
| 130 # We use a semaphore to ensure we don't run more jobs than required. | 136 # We use a semaphore to ensure we don't run more jobs than required. |
| 131 # After each parallel_job finishes, it releases (increments semaphore). | 137 # After each parallel_job finishes, it releases (increments semaphore). |
| 132 for next_parallel_job in parallel_jobs: | 138 for next_parallel_job in parallel_jobs: |
| 133 job_start_semaphore.acquire(block=True) | 139 job_start_semaphore.acquire(block=True) |
| 134 next_parallel_job.start() | 140 next_parallel_job.start() |
| 135 | 141 |
| 136 ParallelJob.WaitUntilJobsComplete(parallel_jobs) | 142 ParallelJob.WaitUntilJobsComplete(parallel_jobs) |
| 137 return [output.get() for output in output_array] | 143 return [output.get() for output in output_array] |
| OLD | NEW |