Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve runtime client config #1314

Merged
merged 10 commits into from
Dec 14, 2023
15 changes: 0 additions & 15 deletions src/ansys/dpf/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,21 +504,6 @@ def release_dpf(self):
else:
error = self._api.data_processing_release(1)

def get_runtime_client_config(self):
anseramosfe marked this conversation as resolved.
Show resolved Hide resolved
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():
Expand Down
3 changes: 3 additions & 0 deletions src/ansys/dpf/core/data_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,9 @@
else:
print(tb)

def to_dict(self):
return self._dict

Check warning on line 657 in src/ansys/dpf/core/data_tree.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/data_tree.py#L657

Added line #L657 was not covered by tests

def __del__(self):
if not self._is_exited:
self._is_exited = True
Expand Down
3 changes: 3 additions & 0 deletions src/ansys/dpf/core/runtime_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@
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())

Check warning on line 138 in src/ansys/dpf/core/runtime_config.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/runtime_config.py#L138

Added line #L138 was not covered by tests


class RuntimeCoreConfig(_RuntimeConfig):
"""Enables to access and set runtime configuration
Expand Down
26 changes: 20 additions & 6 deletions src/ansys/dpf/core/server_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -1057,7 +1072,6 @@ def __init__(
self._input_port = port
self.live = True
self.ansys_path = ansys_path
self._stubs = {}

self._create_shutdown_funcs()

Expand Down Expand Up @@ -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.keys()):
cbellot000 marked this conversation as resolved.
Show resolved Hide resolved
self._stubs[stub_name] = stub_type(self.channel)

def get_stub(self, stub_name):
Expand Down
24 changes: 22 additions & 2 deletions src/ansys/dpf/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
cbellot000 marked this conversation as resolved.
Show resolved Hide resolved
if server is None:
server = core.SERVER
cbellot000 marked this conversation as resolved.
Show resolved Hide resolved
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):
Expand Down
24 changes: 23 additions & 1 deletion src/ansys/dpf/gate/misc.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
DEFAULT_FILE_CHUNK_SIZE = None
COMMON_PROGRESS_BAR = None
_CLIENT_CONFIG = {}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cbellot000 do we have an equivalent PR on pygate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes


class LocalClientConfig(dict):
"""Behaves as a RuntimeClientConfig"""
__delattr__ = dict.__delitem__
__len__ = dict.__len__

def copy_config(self, config):
config._data_tree.add(**self)

def __getattr__(self, key):
if key == "stream_floats_instead_of_doubles":
return self["stream_floats"]
return self.__getitem__(key)

def __setattr__(self, key, value):
if key == "stream_floats_instead_of_doubles":
self["stream_floats"] = value
return self.__setitem__(key, value)


_CLIENT_CONFIG = LocalClientConfig()


def client_config():
if len(_CLIENT_CONFIG) == 0:
_CLIENT_CONFIG["use_cache"] = False
_CLIENT_CONFIG["streaming_buffer_size"] = DEFAULT_FILE_CHUNK_SIZE
_CLIENT_CONFIG["stream_floats"] = False
_CLIENT_CONFIG["return_arrays"] = True
return _CLIENT_CONFIG
24 changes: 24 additions & 0 deletions tests/entry/test_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading