def run_requests(requests_: list, provider_, create_event_loop: bool): if create_event_loop: asyncio.set_event_loop(asyncio.new_event_loop()) results = queue.Queue() done = False try: with session: provider_.run(requests_, results, self._max_concurrent, progress_bar, timeout=self.__timeout) except Exception as e: provider_.enqueue(results, ((k, e) for k in self.__pending.keys())) while self.__pending and not done: done, chunk_results = provider_.drain_queue(results) for (risk_key_, priceable_), result in chunk_results: future = self.__pending.pop((risk_key_, priceable_), None) if future is not None: future.set_result(result) if self.__use_cache: PricingCache.put(risk_key_, priceable_, result) while self.__pending: (risk_key_, _), future = self.__pending.popitem() future.set_result(ErrorValue(risk_key_, 'No result returned'))
def run_requests(requests_: list, provider_, create_event_loop: bool): if create_event_loop: asyncio.set_event_loop(asyncio.new_event_loop()) results = {} try: with session: results = provider_.run(requests_, 1000, progress_bar, timeout=self.__timeout) except Exception as e: results = {k: e for k in self.__pending.keys()} finally: while self.__pending: (risk_key_, priceable_), future = self.__pending.popitem() result = results.get((risk_key_, priceable_)) if result is not None: if self.__use_cache: PricingCache.put(risk_key_, priceable_, result) else: result = ErrorValue(risk_key_, 'No result returned') future.set_result(result)
def _handle_results(cls, request: RiskRequest, results: Union[Iterable, Exception]) -> dict: formatted_results = {} if isinstance(results, Exception): date_results = [{ '$type': 'Error', 'errorString': str(results) }] * len(request.pricing_and_market_data_as_of) position_results = [date_results] * len(request.positions) results = [position_results] * len(request.measures) for risk_measure, position_results in zip(request.measures, results): for position, date_results in zip(request.positions, position_results): for as_of, date_result in zip( request.pricing_and_market_data_as_of, date_results): handler = result_handlers.get(date_result.get('$type')) risk_key = RiskKey(cls, as_of.pricing_date, as_of.market, request.parameters, request.scenario, risk_measure) try: result = handler( date_result, risk_key, position.instrument) if handler else date_result except Exception as e: result = ErrorValue(risk_key, str(e)) _logger.error(result) formatted_results[(risk_key, position.instrument)] = result return formatted_results
def _handle_results(self, results: dict, request: RiskRequest, error: Optional[str] = 'No result returned'): with self.__lock: for risk_measure in request.measures: # Get each risk measure from from the request and the corresponding positions --> futures dict positions_for_measure = self.__futures[(request.scenario, risk_measure)] # Get the results for this measure position_results = results.pop(risk_measure, {}) for position in request.positions: # Set the result for this position to the returned value or an error if missing result = position_results.get(position, ErrorValue(self.pricing_key, error=error)) if self.__use_cache and not isinstance(result, ErrorValue): # Populate the cache PricingCache.put(position.instrument, risk_measure, result) # Retrieve from the cache - this is used by HistoricalPricingContext. We ensure the cache has # all values (in case some had already been computed) then populate the result as the final step result = PricingCache.get(position.instrument, risk_measure) # Set the result for the future positions_for_measure.pop(position).set_result(result) if not positions_for_measure: self.__futures.pop((request.scenario, risk_measure))
def _handle_results(cls, request: RiskRequest, results: Iterable) -> dict: formatted_results = {} pricing_key = PricingKey( request.pricing_and_market_data_as_of, request.pricing_location.value, request.parameters, request.scenario ) for measure_idx, position_results in enumerate(results): risk_measure = request.measures[measure_idx] for position_idx, date_results in enumerate(position_results): if len(date_results) != len(pricing_key): raise RuntimeError('Number of results did not match requested days') handler = result_handlers.get(date_results[0].get('$type')) position = request.positions[position_idx] try: date_results = handler(date_results, pricing_key, position.instrument) if handler else date_results except Exception as e: error = str(e) date_results = ErrorValue(pricing_key, error) _logger.error(error) formatted_results.setdefault(risk_measure, {})[position] = date_results return formatted_results
def _handle_results(cls, request: RiskRequest, results: Iterable) -> dict: formatted_results = {} pricing_key = PricingKey( request.pricing_and_market_data_as_of, request.pricing_location.value, request.parameters, request.scenario ) for measure_idx, position_results in enumerate(results): risk_measure = request.measures[measure_idx] formatter = Formatters.get(risk_measure) if not request.parameters.raw_results else None for position_idx, result in enumerate(position_results): position = request.positions[position_idx] try: result = formatter(result, pricing_key, position.instrument) if formatter else result except Exception as e: error_string = str(e) result = ErrorValue(pricing_key, error_string) _logger.error(error_string) formatted_results.setdefault(risk_measure, {})[position] = result return formatted_results
def _handle_results(cls, request: RiskRequest, results: Iterable) -> dict: formatted_results = {} for risk_measure, position_results in zip(request.measures, results): for position, date_results in zip(request.positions, position_results): for as_of, date_result in zip( request.pricing_and_market_data_as_of, date_results): handler = result_handlers.get(date_result.get('$type')) # TODO Handle this better market = LiveMarket(request.pricing_location) if isinstance(as_of.market_data_as_of, dt.datetime)\ else ClosingMarket(request.pricing_location, as_of.market_data_as_of) risk_key = RiskKey(cls, as_of.pricing_date, market, request.parameters, request.scenario, risk_measure) try: result = handler( date_result, risk_key, position.instrument) if handler else date_result except Exception as e: result = ErrorValue(risk_key, str(e)) _logger.error(result) formatted_results[(risk_key, position.instrument)] = result return formatted_results
def run_request(request: RiskRequest, session: GsSession): calc_result = {} try: with session: calc_result = provider.calc(request) except Exception as e: for risk_measure in request.measures: measure_results = {} for result_position in risk_request.positions: measure_results[result_position] = ErrorValue( self.pricing_key, str(e)) calc_result[risk_measure] = measure_results finally: self._handle_results(calc_result)