Created
June 24, 2015 14:38
-
-
Save offby1/757e27cdf07e7ba4916c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def do_one_device(this_thread): | |
| device_info = this_thread.device | |
| # ... whatever we need, including starting a subprocesses and waiting for it | |
| class DeviceThread(threading.Thread): | |
| def __init__(self, result_queue, device): | |
| super(LoadConfigThread, self).__init__(**kwargs) | |
| self.result_queue = result_queue | |
| self.device = device | |
| def run(self): | |
| try: | |
| do_one_device(self) | |
| except Exception as e: | |
| self.result_queue.put((self.device, e)) | |
| else: | |
| self.result_queue.put((self.device, "a-ok")) | |
| def _multithreaded_device_fiddling(devices): | |
| """Create one thread for each device. Each thread fiddles the device, | |
| and "returns" (that is, enqueues) the result, which is either an | |
| exception or a string; we write each such result into a "summary" | |
| file. | |
| """ | |
| result_queue = Queue.Queue() | |
| threads = [] | |
| for device in devices: | |
| t = DeviceThread(result_queue, | |
| device) | |
| threads.append(t) | |
| for t in threads: | |
| t.daemon = True | |
| t.start() | |
| for t in threads: | |
| t.join() | |
| exceptions = 0 | |
| while True: | |
| try: | |
| device_name, result = result_queue.get(block=False) | |
| if isinstance(result, Exception): | |
| exceptions += 1 | |
| result_by_device[device_name] = result | |
| except Queue.Empty: | |
| break | |
| return result_by_device | |
| def main(): | |
| _multithreaded_device_fiddling(config_ID, rack_id, rack_password, track, zmq_topic, zmq_url, reset_only=reset_only) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment