| 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 def GetCurrentActiveCount(): | 57 def GetCurrentActiveCount(): |
| 58 """Returns the (number of active jobs, first active job).""" | 58 """Returns the (number of active jobs, first active job).""" |
| 59 active_count = 0 | 59 active_count = 0 |
| 60 active_job = None | 60 active_job = None |
| 61 for parallel_job in parallel_jobs: | 61 for parallel_job in parallel_jobs: |
| 62 if parallel_job.is_alive(): | 62 if parallel_job.is_alive(): |
| 63 active_count += 1 | 63 active_count += 1 |
| 64 if not active_job: | 64 if not active_job: |
| 65 active_job = parallel_job | 65 active_job = parallel_job |
| 66 | 66 |
| 67 return (active_count, parallel_job) | 67 return (active_count, active_job) |
| 68 | 68 |
| 69 first_time = True |
| 69 start_time = time.time() | 70 start_time = time.time() |
| 70 while (time.time() - start_time) < cls.MAX_TIMEOUT_SECONDS: | 71 while (time.time() - start_time) < cls.MAX_TIMEOUT_SECONDS: |
| 71 (active_count, active_job) = GetCurrentActiveCount() | 72 (active_count, active_job) = GetCurrentActiveCount() |
| 72 if active_count == 0: | 73 if active_count == 0: |
| 73 return | 74 return |
| 74 else: | 75 else: |
| 75 print >> sys.stderr, ( | 76 if not first_time: |
| 76 'Process Pool Active: Waiting on %d/%d jobs to complete' % | 77 print ( |
| 77 (active_count, len(parallel_jobs))) | 78 'Process Pool Active: Waiting on %d/%d jobs to complete' % |
| 79 (active_count, len(parallel_jobs))) |
| 80 else: |
| 81 first_time = False |
| 82 |
| 78 active_job.join(cls.SLEEP_TIMEOUT_SECONDS) | 83 active_job.join(cls.SLEEP_TIMEOUT_SECONDS) |
| 79 time.sleep(5) # Prevents lots of printing out as job is ending. | 84 time.sleep(5) # Prevents lots of printing out as job is ending. |
| 80 | 85 |
| 81 for parallel_job in parallel_jobs: | 86 for parallel_job in parallel_jobs: |
| 82 if parallel_job.is_alive(): | 87 if parallel_job.is_alive(): |
| 83 parallel_job.terminate() | 88 parallel_job.terminate() |
| 84 | 89 |
| 85 raise ParallelJobTimeoutError('Exceeded max time of %d seconds to wait for ' | 90 raise ParallelJobTimeoutError('Exceeded max time of %d seconds to wait for ' |
| 86 'job completion.' % cls.MAX_TIMEOUT_SECONDS) | 91 'job completion.' % cls.MAX_TIMEOUT_SECONDS) |
| 87 | 92 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 output_array.append(output) | 141 output_array.append(output) |
| 137 | 142 |
| 138 # We use a semaphore to ensure we don't run more jobs than required. | 143 # We use a semaphore to ensure we don't run more jobs than required. |
| 139 # After each parallel_job finishes, it releases (increments semaphore). | 144 # After each parallel_job finishes, it releases (increments semaphore). |
| 140 for next_parallel_job in parallel_jobs: | 145 for next_parallel_job in parallel_jobs: |
| 141 job_start_semaphore.acquire(block=True) | 146 job_start_semaphore.acquire(block=True) |
| 142 next_parallel_job.start() | 147 next_parallel_job.start() |
| 143 | 148 |
| 144 ParallelJob.WaitUntilJobsComplete(parallel_jobs) | 149 ParallelJob.WaitUntilJobsComplete(parallel_jobs) |
| 145 return [output.get() for output in output_array] | 150 return [output.get() for output in output_array] |
| OLD | NEW |