def test28(self): x = self.ForTestingInterface() def next_request(index): if index < 3: return x.sleep.asynchronous(0.1) else: return None sequence = ASyncRequestSequence(next_request) self.assertFalse(sequence.is_finished) sequence.wait() self.assertTrue(sequence.is_finished) result = sequence.result() self.assertEqual(len(result), 3) x.stop()
def set_values_in_store_async(self, indices, attributes, quantities): array_of_indices = self._to_arrays_of_indices(indices) one_dimensional_array_of_indices = [ x.reshape(-1) for x in array_of_indices ] if len(one_dimensional_array_of_indices) == 0: one_dimensional_values = [x for x in quantities] else: one_dimensional_values = [(x.reshape(-1) if is_quantity(x) else numpy.asanyarray(x).reshape(-1)) for x in quantities] selected_setters = list( [setter for setter in self.select_setters_for(attributes)]) def next_request(index, setters): if index < len(setters): setter = setters[index] return setter.set_attribute_values_async( self, attributes, one_dimensional_values, *one_dimensional_array_of_indices) else: return None request = ASyncRequestSequence(next_request, args=(selected_setters, )) return request
def set_values_in_store_async(self, indices_in_the_code, attributes, values): if indices_in_the_code is None: indices_in_the_code = self.code_indices if len(indices_in_the_code) == 0: return setters = self.select_setters_for(attributes) if len(setters) > 1: def new_request(index, setters, attributes, values, indices_in_the_code): if index >= len(setters): return None setter = setters[index] request = setter.set_attribute_values_async( self, attributes, values, indices_in_the_code) return request request = ASyncRequestSequence(new_request, args=(setters, attributes, values, indices_in_the_code)) else: for setter in setters: request = setter.set_attribute_values(self, attributes, values, indices_in_the_code) return request
def test29(self): pool = AsyncRequestsPool() x = self.ForTestingInterface() y = self.ForTestingInterface() sequenced_requests_indices = [] def next_request(index): if index < 4: sequenced_requests_indices.append(index) return x.sleep.asynchronous(0.5) else: return None request1 = ASyncRequestSequence(next_request) request2 = y.sleep.asynchronous(1.0) finished_requests = [] def handle_result(request, index): self.assertTrue(request.is_result_available()) self.assertTrue(request.is_finished) finished_requests.append(index) pool.add_request(request1, handle_result, [1]) pool.add_request(request2, handle_result, [2]) pool.wait() self.assertEquals(len(finished_requests), 1) self.assertEquals(len(pool), 1) self.assertEquals(finished_requests, [2]) self.assertTrue(len(sequenced_requests_indices) > 0) pool.wait() self.assertEquals(len(finished_requests), 2) self.assertEquals(len(pool), 0) x.sleep(0.1) self.assertEquals(sequenced_requests_indices, [0, 1, 2, 3]) self.assertTrue(request1.is_result_available()) self.assertTrue(request2.is_result_available()) self.assertEquals(request1.result(), [0, 0, 0, 0]) self.assertEquals(request2.result(), 0) y.stop() x.stop()
def get_values_in_store_async(self, indices_in_the_code, attributes): if indices_in_the_code is None: indices_in_the_code = self.code_indices if len(indices_in_the_code) == 0: return [[] for attribute in attributes] mapping_from_attribute_to_result = {} getters = self.select_getters_for(attributes) if len(getters) > 1: def new_request(index, getters, attributes, indices_in_the_code): if index >= len(getters): return None getter = getters[index] request = getter.get_attribute_values_async( self, attributes, indices_in_the_code) def result_handler(inner, mapping): mapping.update(inner()) request.add_result_handler( result_handler, (mapping_from_attribute_to_result, )) return request request = ASyncRequestSequence(new_request, args=(getters, attributes, indices_in_the_code)) else: for getter in getters: request = getter.get_attribute_values_async( self, attributes, indices_in_the_code) def result_handler(inner, mapping): mapping.update(inner()) request.add_result_handler( result_handler, (mapping_from_attribute_to_result, )) def all_handler(inner, mapping): inner() results = [] for attribute in attributes: results.append(mapping[attribute]) return results request.add_result_handler(all_handler, (mapping_from_attribute_to_result, )) return request