Skip to content

Instantly share code, notes, and snippets.

@offby1
Created June 24, 2015 14:38
Show Gist options
  • Select an option

  • Save offby1/757e27cdf07e7ba4916c to your computer and use it in GitHub Desktop.

Select an option

Save offby1/757e27cdf07e7ba4916c to your computer and use it in GitHub Desktop.
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