def CreateValue(self, request, context): """Creates a value embedded in the executor. Args: request: An instance of `executor_pb2.CreateValueRequest`. context: An instance of `grpc.ServicerContext`. Returns: An instance of `executor_pb2.CreateValueResponse`. """ py_typecheck.check_type(request, executor_pb2.CreateValueRequest) try: value, value_type = (executor_service_utils.deserialize_value( request.value)) value_id = str(uuid.uuid4()) future_val = asyncio.run_coroutine_threadsafe( self._executor.create_value(value, value_type), self._event_loop) with self._lock: self._values[value_id] = future_val return executor_pb2.CreateValueResponse( value_ref=executor_pb2.ValueRef(id=value_id)) except (ValueError, TypeError) as err: logging.error(traceback.format_exc()) context.set_code(grpc.StatusCode.INVALID_ARGUMENT) context.set_details(str(err)) return executor_pb2.CreateValueResponse()
def test_create_value_returns_value(self, mock_executor_grpc_stub): response = executor_pb2.CreateValueResponse() instance = mock_executor_grpc_stub.return_value instance.CreateValue = mock.Mock(side_effect=[response]) stub = create_stub() result = stub.create_value(request=executor_pb2.CreateValueRequest()) self.assertEqual(result, response)
def test_create_value_returns_remote_value(self, mock_stub): response = executor_pb2.ExecuteResponse( create_value=executor_pb2.CreateValueResponse()) executor = _setup_mock_streaming_executor(mock_stub, response) loop = asyncio.get_event_loop() result = loop.run_until_complete(executor.create_value(1, tf.int32)) self.assertIsInstance(result, remote_executor.RemoteValue)
def test_create_value_returns_remote_value(self, mock_stub): mock_stub.create_value.return_value = executor_pb2.CreateValueResponse( ) executor = remote_executor.RemoteExecutor(mock_stub) _set_cardinalities_with_mock(executor, mock_stub) result = asyncio.run(executor.create_value(1, tf.int32)) mock_stub.create_value.assert_called_once() self.assertIsInstance(result, remote_executor.RemoteValue)
def test_create_value_raises_retryable_error_on_grpc_error_unavailable( self, mock_stub): response = executor_pb2.ExecuteResponse( create_value=executor_pb2.CreateValueResponse()) executor = _setup_mock_streaming_executor( mock_stub, response, error_fn=_raise_grpc_error_unavailable) loop = asyncio.get_event_loop() with self.assertRaises(execution_context.RetryableError): loop.run_until_complete(executor.create_value(1, tf.int32))
def test_create_value_returns_remote_value(self, mock_stub): response = executor_pb2.CreateValueResponse() instance = mock_stub.return_value instance.CreateValue = mock.Mock(side_effect=[response]) loop = asyncio.get_event_loop() executor = create_remote_executor() result = loop.run_until_complete(executor.create_value(1, tf.int32)) instance.CreateValue.assert_called_once() self.assertIsInstance(result, remote_executor.RemoteValue)
def test_create_value_raises_non_retryable_error(self, mock_stub): response = executor_pb2.ExecuteResponse( create_value=executor_pb2.CreateValueResponse()) executor = _setup_mock_streaming_executor( mock_stub, response, error_fn=_raise_non_retryable_grpc_error) loop = asyncio.get_event_loop() with self.assertRaises(grpc.RpcError) as context: loop.run_until_complete(executor.create_value(1, tf.int32)) self.assertEqual(context.exception.code(), grpc.StatusCode.ABORTED)
def CreateValue( self, request: executor_pb2.CreateValueRequest, context: grpc.ServicerContext, ) -> executor_pb2.CreateValueResponse: """Creates a value embedded in the executor.""" py_typecheck.check_type(request, executor_pb2.CreateValueRequest) try: value, value_type = ( executor_service_utils.deserialize_value(request.value)) value_id = str(uuid.uuid4()) future_val = asyncio.run_coroutine_threadsafe( self._executor.create_value(value, value_type), self._event_loop) with self._lock: self._values[value_id] = future_val return executor_pb2.CreateValueResponse( value_ref=executor_pb2.ValueRef(id=value_id)) except (ValueError, TypeError) as err: _set_invalid_arg_err(context, err) return executor_pb2.CreateValueResponse()
def CreateValue( self, request: executor_pb2.CreateValueRequest, context: grpc.ServicerContext, ) -> executor_pb2.CreateValueResponse: """Creates a value embedded in the executor.""" py_typecheck.check_type(request, executor_pb2.CreateValueRequest) try: with tracing.span('ExecutorService.CreateValue', 'deserialize_value'): value, value_type = ( executor_service_utils.deserialize_value(request.value)) value_id = str(uuid.uuid4()) coro = self._executor.create_value(value, value_type) future_val = self._run_coro_threadsafe_with_tracing(coro) with self._lock: self._values[value_id] = future_val return executor_pb2.CreateValueResponse( value_ref=executor_pb2.ValueRef(id=value_id)) except (ValueError, TypeError) as err: _set_invalid_arg_err(context, err) return executor_pb2.CreateValueResponse()
def CreateValue(self, request, context): """Creates a value embedded in the executor. Args: request: An instance of `executor_pb2.CreateValueRequest`. context: An instance of `grpc.ServicerContext`. Returns: An instance of `executor_pb2.CreateValueResponse`. """ py_typecheck.check_type(request, executor_pb2.CreateValueRequest) try: value, value_type = (executor_service_utils.deserialize_value( request.value)) value_id = str(uuid.uuid4()) future_val = self._executor.create_value(value, value_type) with self._lock: self._values[value_id] = future_val return executor_pb2.CreateValueResponse( value_ref=executor_pb2.ValueRef(id=value_id)) except (ValueError, TypeError): context.set_code(grpc.StatusCode.INVALID_ARGUMENT) return executor_pb2.CreateValueResponse()
def CreateValue( self, request: executor_pb2.CreateValueRequest, context: grpc.ServicerContext, ) -> executor_pb2.CreateValueResponse: """Creates a value embedded in the executor.""" py_typecheck.check_type(request, executor_pb2.CreateValueRequest) with self._try_handle_request_context( request, context, executor_pb2.CreateValueResponse): with tracing.span('ExecutorService.CreateValue', 'deserialize_value'): value, value_type = (value_serialization.deserialize_value( request.value)) value_id = str(uuid.uuid4()) coro = self.executor(request, context).create_value(value, value_type) future_val = self._run_coro_threadsafe_with_tracing(coro) with self._lock: self._values[value_id] = future_val return executor_pb2.CreateValueResponse( value_ref=executor_pb2.ValueRef(id=value_id))