class DeviceThread(Thread): """ Class that implements the device's worker thread. """ def __init__(self, device): """ Constructor. @type device: Device @param device: the device which owns this thread """ Thread.__init__(self, name="Device Thread %d" % device.device_id) self.device = device self.thread_pool = ThreadPool(8) def run(self): self.thread_pool.set_device(self.device) while True: # get the current neighbourhood neighbours = self.device.supervisor.get_neighbours() if neighbours is None: break # stay in this loop until the timepoint ends while True: # if we already have scripts or the timepoint is finished if self.device.scripts_arrived or self.device.timepoint_done.wait( ): if self.device.scripts_arrived: self.device.scripts_arrived = False # run scripts received until now for (script, location) in self.device.scripts: self.thread_pool.submit(neighbours, script, location) else: self.device.timepoint_done.clear() self.device.scripts_arrived = True break # wait for thread pool to finish it's job self.thread_pool.wait_threads() # wait for all devices to reach this point self.device.barrier.wait() # close all threads in the thread pool self.thread_pool.end_threads()