diff --git a/src/ansys/dpf/core/core.py b/src/ansys/dpf/core/core.py index 2c20ba840a..36788d9220 100644 --- a/src/ansys/dpf/core/core.py +++ b/src/ansys/dpf/core/core.py @@ -504,21 +504,6 @@ def release_dpf(self): else: error = self._api.data_processing_release(1) - def get_runtime_client_config(self): - if self._server().has_client(): - data_tree_tmp = self._api.data_processing_get_client_config_as_data_tree() - config_to_return = RuntimeClientConfig(data_tree=data_tree_tmp, server=self._server()) - else: - if misc.RUNTIME_CLIENT_CONFIG is None: - from ansys.dpf.core import data_tree - - misc.RUNTIME_CLIENT_CONFIG = RuntimeClientConfig( - data_tree=data_tree.DataTree(server=self._server()) - ) - misc.RUNTIME_CLIENT_CONFIG._data_tree._holds_server = False - config_to_return = misc.RUNTIME_CLIENT_CONFIG - return config_to_return - @version_requires("4.0") def get_runtime_core_config(self): if self._server().has_client(): diff --git a/src/ansys/dpf/core/data_tree.py b/src/ansys/dpf/core/data_tree.py index faa5e4adda..de1b7ac6d8 100644 --- a/src/ansys/dpf/core/data_tree.py +++ b/src/ansys/dpf/core/data_tree.py @@ -653,6 +653,9 @@ def __exit__(self, type_to_use, value, tb): else: print(tb) + def to_dict(self): + return self._dict + def __del__(self): if not self._is_exited: self._is_exited = True diff --git a/src/ansys/dpf/core/runtime_config.py b/src/ansys/dpf/core/runtime_config.py index ba08ba3fda..eb1a234948 100644 --- a/src/ansys/dpf/core/runtime_config.py +++ b/src/ansys/dpf/core/runtime_config.py @@ -134,6 +134,9 @@ def return_arrays(self): def return_arrays(self, value): self._data_tree.add(return_arrays=int(value)) + def copy_config(self, config): + config._data_tree.add(self._data_tree.to_dict()) + class RuntimeCoreConfig(_RuntimeConfig): """Enables to access and set runtime configuration diff --git a/src/ansys/dpf/core/server_types.py b/src/ansys/dpf/core/server_types.py index 1f888d702b..8bf6a505c0 100644 --- a/src/ansys/dpf/core/server_types.py +++ b/src/ansys/dpf/core/server_types.py @@ -640,12 +640,20 @@ def __del__(self): class GrpcClient: - def __init__(self, address=None): + def __init__(self): from ansys.dpf.gate import client_capi - self._internal_obj = client_capi.ClientCAPI.client_new_full_address(address) client_capi.ClientCAPI.init_client_environment(self) + def set_address(self, address, server): + from ansys.dpf.core import misc, settings + if misc.RUNTIME_CLIENT_CONFIG is not None: + self_config = settings.get_runtime_client_config(server=server) + misc.RUNTIME_CLIENT_CONFIG.copy_config(self_config) + from ansys.dpf.gate import client_capi + self._internal_obj = client_capi.ClientCAPI.client_new_full_address(address) + + def __del__(self): try: self._deleter_func[0](self._deleter_func[1](self)) @@ -676,6 +684,8 @@ def __init__( super().__init__(ansys_path=ansys_path, load_operators=load_operators) # Load Ans.Dpf.GrpcClient self._grpc_client_path = load_api.load_grpc_client(ansys_path=ansys_path) + + self._client = GrpcClient() self._own_process = launch_server self._local_server = False self._os = None @@ -709,8 +719,8 @@ def __init__( launch_dpf(ansys_path, ip, port, timeout=timeout) self._local_server = True - self._client = GrpcClient(address) # store port and ip for later reference + self._client.set_address(address, self) self._address = address self._input_ip = ip self._input_port = port @@ -1011,6 +1021,8 @@ def __init__( self._own_process = launch_server self.live = False self._local_server = False + self._stubs = {} + self.channel = None # Load Ans.Dpf.Grpc? import grpc @@ -1048,7 +1060,10 @@ def __init__( else: launch_dpf(ansys_path, ip, port, timeout=timeout) self._local_server = True - + from ansys.dpf.core import misc, settings + if misc.RUNTIME_CLIENT_CONFIG is not None: + self_config = settings.get_runtime_client_config(server=self) + misc.RUNTIME_CLIENT_CONFIG.copy_config(self_config) self.channel = grpc.insecure_channel(address) # store the address for later reference @@ -1057,7 +1072,6 @@ def __init__( self._input_port = port self.live = True self.ansys_path = ansys_path - self._stubs = {} self._create_shutdown_funcs() @@ -1086,7 +1100,7 @@ def get_api_for_type(self, capi, grpcapi): return grpcapi def create_stub_if_necessary(self, stub_name, stub_type): - if not (stub_name in self._stubs.keys()): + if self.channel and not stub_name in self._stubs: self._stubs[stub_name] = stub_type(self.channel) def get_stub(self, stub_name): diff --git a/src/ansys/dpf/core/settings.py b/src/ansys/dpf/core/settings.py index c451f3fc2a..63497ef301 100644 --- a/src/ansys/dpf/core/settings.py +++ b/src/ansys/dpf/core/settings.py @@ -10,6 +10,10 @@ from ansys.dpf.core.server_context import set_default_server_context # noqa: F401 from ansys.dpf.core.server_factory import ServerConfig # noqa: F401 from ansys.dpf.core import core +from ansys.dpf.gate import ( + data_processing_capi, + data_processing_grpcapi, +) def disable_off_screen_rendering() -> None: @@ -121,8 +125,24 @@ def get_runtime_client_config(server=None): with Ans.Dpf.GrpcClient configuration. """ - base = core.BaseService(server, load_operators=False) - return base.get_runtime_client_config() + from ansys.dpf.core.runtime_config import RuntimeClientConfig + from ansys.dpf import core as root + if server is None: + server = root.SERVER + if server is not None and server.has_client(): + _api = server.get_api_for_type( + capi=data_processing_capi.DataProcessingCAPI, + grpcapi=data_processing_grpcapi.DataProcessingGRPCAPI, + ) + _api.init_data_processing_environment(server) # creates stub when gRPC + data_tree_tmp = _api.data_processing_get_client_config_as_data_tree() + config_to_return = RuntimeClientConfig(data_tree=data_tree_tmp, server=server) + else: + if misc.RUNTIME_CLIENT_CONFIG is None: + from ansys.dpf.gate import misc as gate_misc + misc.RUNTIME_CLIENT_CONFIG = gate_misc.client_config() + config_to_return = misc.RUNTIME_CLIENT_CONFIG + return config_to_return def get_runtime_core_config(server=None): diff --git a/src/ansys/dpf/gate/misc.py b/src/ansys/dpf/gate/misc.py index ec3b1e0568..fcb2ac528b 100644 --- a/src/ansys/dpf/gate/misc.py +++ b/src/ansys/dpf/gate/misc.py @@ -27,4 +27,4 @@ def client_config(): _CLIENT_CONFIG.streaming_buffer_size = DEFAULT_FILE_CHUNK_SIZE _CLIENT_CONFIG.stream_floats_instead_of_doubles = False _CLIENT_CONFIG.return_arrays = True - return _CLIENT_CONFIG + return _CLIENT_CONFIG \ No newline at end of file diff --git a/tests/entry/test_entry.py b/tests/entry/test_entry.py index da6aad6c47..51aef7ad42 100644 --- a/tests/entry/test_entry.py +++ b/tests/entry/test_entry.py @@ -105,6 +105,30 @@ def test_apply_context_remote(remote_config_server_type): assert dpf.SERVER.context == dpf.AvailableServerContexts.premium +@pytest.mark.order(5) +@conftest.raises_for_servers_version_under("4.0") +def test_runtime_client_no_server(remote_config_server_type): + dpf.server.shutdown_all_session_servers() + dpf.SERVER_CONFIGURATION = remote_config_server_type + client_config = dpf.settings.get_runtime_client_config() + initial = client_config.stream_floats_instead_of_doubles + client_config.stream_floats_instead_of_doubles = True + server = dpf.start_local_server(as_global=False) + client_config = dpf.settings.get_runtime_client_config(server) + assert client_config.stream_floats_instead_of_doubles is True + + + server = dpf.connect_to_server( + ip=server.ip, port=server.port, as_global=False) + client_config = dpf.settings.get_runtime_client_config(server) + assert client_config.stream_floats_instead_of_doubles is True + + dpf.server.shutdown_all_session_servers() + client_config = dpf.settings.get_runtime_client_config() + client_config.stream_floats_instead_of_doubles = initial + assert client_config.stream_floats_instead_of_doubles == initial + + @pytest.mark.order("last") # Mandatory @pytest.mark.skipif( running_docker or not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_5_0,