Christoffer Quist Adamsen | 65ef298 | 2023-08-24 08:45:39 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright (c) 2023, the R8 project authors. Please see the AUTHORS file |
| 3 | # for details. All rights reserved. Use of this source code is governed by a |
| 4 | # BSD-style license that can be found in the LICENSE file. |
| 5 | |
| 6 | import sys |
| 7 | import threading |
| 8 | from threading import Thread |
| 9 | import traceback |
| 10 | |
| 11 | # A thread that is given a list of jobs. The thread will repeatedly take a job |
| 12 | # from the list of jobs (which is shared with other threads) and execute it |
| 13 | # until there is no more jobs. |
| 14 | # |
| 15 | # If stop_on_first_failure is True, then the thread will exit upon the first |
| 16 | # failing job. The thread will then clear the jobs to ensure that all other |
| 17 | # workers will also terminate after completing there current job. |
| 18 | # |
| 19 | # Each job is a lambda that takes the worker_id as an argument. To guarantee |
| 20 | # termination each job must itself terminate (i.e., each job is responsible for |
| 21 | # setting an appropriate timeout). |
| 22 | class WorkerThread(Thread): |
| 23 | |
| 24 | # The initialization of a WorkerThread is never run concurrently with the |
| 25 | # initialization of other WorkerThreads. |
| 26 | def __init__(self, jobs, jobs_lock, stop_on_first_failure, worker_id): |
| 27 | Thread.__init__(self) |
Christoffer Quist Adamsen | 60d7370 | 2023-09-05 12:05:47 +0200 | [diff] [blame] | 28 | self.completed = False |
Christoffer Quist Adamsen | 65ef298 | 2023-08-24 08:45:39 +0200 | [diff] [blame] | 29 | self.jobs = jobs |
| 30 | self.jobs_lock = jobs_lock |
| 31 | self.number_of_jobs = len(jobs) |
| 32 | self.stop_on_first_failure = stop_on_first_failure |
| 33 | self.success = True |
| 34 | self.worker_id = worker_id |
| 35 | |
| 36 | def run(self): |
| 37 | print_thread("Starting worker", self.worker_id) |
| 38 | while True: |
| 39 | (job, job_id) = self.take_job(self.jobs, self.jobs_lock) |
| 40 | if job is None: |
| 41 | break |
| 42 | try: |
| 43 | print_thread( |
| 44 | "Starting job %s/%s" % (job_id, self.number_of_jobs), |
| 45 | self.worker_id) |
| 46 | exit_code = job(self.worker_id) |
| 47 | print_thread( |
| 48 | "Job %s finished with exit code %s" |
| 49 | % (job_id, exit_code), |
| 50 | self.worker_id) |
| 51 | if exit_code: |
| 52 | self.success = False |
| 53 | if self.stop_on_first_failure: |
Christoffer Quist Adamsen | 60d7370 | 2023-09-05 12:05:47 +0200 | [diff] [blame] | 54 | self.clear_jobs(self.jobs, self.jobs_lock) |
Christoffer Quist Adamsen | 65ef298 | 2023-08-24 08:45:39 +0200 | [diff] [blame] | 55 | break |
| 56 | except: |
| 57 | print_thread("Job %s crashed" % job_id, self.worker_id) |
| 58 | print_thread(traceback.format_exc(), self.worker_id) |
| 59 | self.success = False |
| 60 | if self.stop_on_first_failure: |
Christoffer Quist Adamsen | 60d7370 | 2023-09-05 12:05:47 +0200 | [diff] [blame] | 61 | self.clear_jobs(self.jobs, self.jobs_lock) |
Christoffer Quist Adamsen | 65ef298 | 2023-08-24 08:45:39 +0200 | [diff] [blame] | 62 | break |
| 63 | print_thread("Exiting", self.worker_id) |
Christoffer Quist Adamsen | 60d7370 | 2023-09-05 12:05:47 +0200 | [diff] [blame] | 64 | self.completed = True |
Christoffer Quist Adamsen | 65ef298 | 2023-08-24 08:45:39 +0200 | [diff] [blame] | 65 | |
| 66 | def take_job(self, jobs, jobs_lock): |
| 67 | jobs_lock.acquire() |
| 68 | job_id = self.number_of_jobs - len(jobs) + 1 |
| 69 | job = jobs.pop(0) if jobs else None |
| 70 | jobs_lock.release() |
| 71 | return (job, job_id) |
| 72 | |
| 73 | def clear_jobs(self, jobs, jobs_lock): |
| 74 | jobs_lock.acquire() |
| 75 | jobs.clear() |
| 76 | jobs_lock.release() |
| 77 | |
| 78 | def run_in_parallel(jobs, number_of_workers, stop_on_first_failure): |
| 79 | assert number_of_workers > 0 |
| 80 | if number_of_workers > len(jobs): |
| 81 | number_of_workers = len(jobs) |
| 82 | if number_of_workers == 1: |
| 83 | return run_in_sequence(jobs, stop_on_first_failure) |
| 84 | jobs_lock = threading.Lock() |
| 85 | threads = [] |
| 86 | for worker_id in range(1, number_of_workers + 1): |
| 87 | threads.append( |
| 88 | WorkerThread(jobs, jobs_lock, stop_on_first_failure, worker_id)) |
| 89 | for thread in threads: |
| 90 | thread.start() |
| 91 | for thread in threads: |
| 92 | thread.join() |
| 93 | for thread in threads: |
Christoffer Quist Adamsen | 60d7370 | 2023-09-05 12:05:47 +0200 | [diff] [blame] | 94 | if not thread.completed or not thread.success: |
Christoffer Quist Adamsen | 65ef298 | 2023-08-24 08:45:39 +0200 | [diff] [blame] | 95 | return 1 |
| 96 | return 0 |
| 97 | |
| 98 | def run_in_sequence(jobs, stop_on_first_failure): |
| 99 | combined_exit_code = 0 |
| 100 | worker_id = None |
| 101 | for job in jobs: |
| 102 | try: |
| 103 | exit_code = job(worker_id) |
| 104 | if exit_code: |
| 105 | combined_exit_code = exit_code |
| 106 | if stop_on_first_failure: |
| 107 | break |
| 108 | except: |
| 109 | print(traceback.format_exc()) |
| 110 | combined_exit_code = 1 |
| 111 | if stop_on_first_failure: |
| 112 | break |
| 113 | return combined_exit_code |
| 114 | |
| 115 | def print_thread(msg, worker_id): |
| 116 | if worker_id is None: |
| 117 | print(msg) |
Christoffer Quist Adamsen | cca1aa3 | 2023-08-24 09:17:37 +0200 | [diff] [blame] | 118 | else: |
| 119 | print('WORKER %s: %s' % (worker_id, msg)) |