def _attr_forker(*args, **kwargs): work = [] back_map = [] for service_info, instance in self.instances.items(): current_work = forkjoin.WorkItem(getattr(instance, attr), *args, **kwargs) work.append(current_work) back_map.append(service_info) # maps work idx->result results = forkjoin.fork_join(work) # maps connection_info->result output = {} exceptions = {} for idx, result in enumerate(results): connection_info = back_map[idx] output[connection_info] = result if isinstance(result, BaseException): exceptions[idx] = result assert not exceptions, "ERROR: exceptions while " \ "running: %s" % str(exceptions) return output
def spawn_many(self, connection_infos=None): """ Spawn and attach a bunch of instances. :param iterable connection_infos: if None, we will spawn using the :class:`ConnectionInfo` s passed to the initializer. Otherwise, ``connection_infos`` should be an iterable of ``ConnectionInfos``. """ work = [] if connection_infos is not None: self._connection_infos = copy.copy(connection_infos) for connection_info in self._connection_infos: work_item = forkjoin.WorkItem(self.spawn, connection_info) work.append(work_item) results = forkjoin.fork_join(work) for idx, result in enumerate(results): if isinstance(result, BaseException): connection_info = self._connection_infos[idx] sys.stderr.write("ERROR spawning (%s): " % repr(connection_info)) raise result
def test_all_pass(self): count = 100 work = [] for idx in range(count): wi = forkjoin.WorkItem(my_inner_func, idx) work.append(wi) results = forkjoin.fork_join(work) self.assertEqual(results, list(range(100)))
def test_some_fail(self): count = 100 work = [] for idx in range(count): if idx in (1, 50, 99): wi = forkjoin.WorkItem(my_inner_func, idx, fail=True) else: wi = forkjoin.WorkItem(my_inner_func, idx) work.append(wi) results = forkjoin.fork_join(work, suppress_errors=True) for idx in range(count): if idx in (1, 50, 99): self.assertTrue(isinstance(results[idx], RuntimeError)) else: self.assertEqual(results[idx], idx)
def shutdown(self, hard=False): """ Shutdown all instances this array is tracking. :param bool hard: if False, we will assert that instances shut down cleanly, otherwise we won't. """ work = [] keys = copy.copy(list(self.instances.keys())) for key in keys: work_item = forkjoin.WorkItem(self._shutdown_single, key) work.append(work_item) results = forkjoin.fork_join(work) if not hard: for idx, result in enumerate(results): if isinstance(result, BaseException): key = keys[idx] sys.stderr.write("ERROR shutting down (%s): " % key) raise result self.INSTANCES.remove(self)