def on_get(self, req, resp, model_name, requested_version=0): logger.debug("MODEL_STATUS, get request: {}, {}".format( model_name, requested_version)) valid_model_status = check_availability_of_requested_status( models=self.models, requested_version=requested_version, model_name=model_name) if not valid_model_status: resp.status = falcon.HTTP_NOT_FOUND logger.debug("MODEL_STATUS, invalid model spec from request") err_out_json = { 'error': WRONG_MODEL_SPEC.format(model_name, requested_version) } resp.body = json.dumps(err_out_json) return requested_version = int(requested_version) response = get_model_status_pb2.GetModelStatusResponse() if requested_version: version_status = self.models[model_name].versions_statuses[ requested_version] ModelServiceServicer.add_status_to_response( version_status, response) else: for version_status in self.models[model_name].versions_statuses. \ values(): ModelServiceServicer.add_status_to_response( version_status, response) logger.debug("MODEL_STATUS created a response for {} - {}".format( model_name, requested_version)) resp.status = falcon.HTTP_200 resp.body = MessageToJson(response, including_default_value_fields=True)
def serve(models, max_workers: int = 1, port: int = 9000): server = grpc.server(futures.ThreadPoolExecutor(max_workers=max_workers), options=[('grpc.max_send_message_length', GIGABYTE), ('grpc.max_receive_message_length', GIGABYTE) ]) prediction_service_pb2_grpc.add_PredictionServiceServicer_to_server( PredictionServiceServicer(models=models), server) model_service_pb2_grpc.add_ModelServiceServicer_to_server( ModelServiceServicer(models=models), server) server.add_insecure_port('[::]:{}'.format(port)) server.start() logger.info("Server listens on port {port} and will be " "serving models: {models}".format(port=port, models=list(models.keys()))) try: while True: if FILE_SYSTEM_POLL_WAIT_SECONDS > 0: time.sleep(FILE_SYSTEM_POLL_WAIT_SECONDS) for model in models: models[model].update() else: time.sleep(_ONE_DAY_IN_SECONDS) except KeyboardInterrupt: server.stop(0) sys.exit(0)
def get_grpc_service_for_model_status(get_fake_model): _real_time = grpc_testing.strict_real_time() servicer = ModelServiceServicer(models={'test': get_fake_model}) descriptors_to_servicers = {MODEL_SERVICE: servicer} _real_time_server = grpc_testing.server_from_dictionary( descriptors_to_servicers, _real_time) return _real_time_server