.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\00-basic\11-server_types.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_00-basic_11-server_types.py: .. _ref_server_types_example: Communicate in process or via gRPC ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Starting with Ansys 2022 R2, PyDPF can communicate either via In Process or via gRPC with DPF C++ core server (``Ans.Dpf.Grpc.exe``). To choose which type of :class:`ansys.dpf.core.server_types.BaseServer` (object defining the type of communication and the server instance to communicate with) to use, a :class:`ansys.dpf.core.server_factory.ServerConfig` class should be used. Until Ansys 2022R1, only gRPC communication using python module ``ansys.grpc.dpf`` is supported (now called :class:`ansys.dpf.core.server_types.LegacyGrpcServer`), starting with Ansys 2022 R2, three types of servers are supported: - :class:`ansys.dpf.core.server_types.InProcessServer` loading DPF in process. Cannot run on Docker. - :class:`ansys.dpf.core.server_types.GrpcServer` using gRPC communication through the DPF gRPC CLayer ``Ans.Dpf.GrpcClient``. - :class:`ansys.dpf.core.server_types.LegacyGrpcServer` using gRPC communication through the Python module ``ansys.grpc.dpf``. To change the default type of server's configuration used by DPF change: - the global variable ``SERVER_CONFIGURATION`` at the beginning of the python script. - the environment variable ``DPF_SERVER_TYPE`` before running the python executable, see :class:`ansys.dpf.core.server_factory.ServerConfig` for more information. .. GENERATED FROM PYTHON SOURCE LINES 32-36 .. code-block:: default import os from ansys.dpf import core as dpf .. GENERATED FROM PYTHON SOURCE LINES 37-39 Start servers with custom server configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 39-49 .. code-block:: default in_process_config = dpf.AvailableServerConfigs.InProcessServer grpc_config = dpf.AvailableServerConfigs.GrpcServer legacy_grpc_config = dpf.AvailableServerConfigs.LegacyGrpcServer if "DPF_DOCKER" not in os.environ.keys(): in_process_server = dpf.start_local_server(config=in_process_config) grpc_server = dpf.start_local_server(config=grpc_config) legacy_grpc_server = dpf.start_local_server(config=legacy_grpc_config) .. GENERATED FROM PYTHON SOURCE LINES 50-51 Equivalent to: .. GENERATED FROM PYTHON SOURCE LINES 51-65 .. code-block:: default in_process_config = dpf.ServerConfig(protocol=None, legacy=False) grpc_config = dpf.ServerConfig( protocol=dpf.server_factory.CommunicationProtocols.gRPC, legacy=False ) legacy_grpc_config = dpf.ServerConfig( protocol=dpf.server_factory.CommunicationProtocols.gRPC, legacy=True ) if "DPF_DOCKER" not in os.environ.keys(): in_process_server = dpf.start_local_server(config=in_process_config, as_global=False) grpc_server = dpf.start_local_server(config=grpc_config, as_global=False) legacy_grpc_server = dpf.start_local_server(config=legacy_grpc_config, as_global=False) .. GENERATED FROM PYTHON SOURCE LINES 66-68 Create data on different servers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 68-85 .. code-block:: default if "DPF_DOCKER" not in os.environ.keys(): in_process_field = dpf.fields_factory.create_scalar_field(2, server=in_process_server) in_process_field.append([1.0], 1) in_process_field.append([2.0], 2) grpc_field = dpf.fields_factory.create_scalar_field(2, server=grpc_server) grpc_field.append([1.0], 1) grpc_field.append([2.0], 2) legacy_grpc_field = dpf.fields_factory.create_scalar_field(2, server=legacy_grpc_server) legacy_grpc_field.append([1.0], 1) legacy_grpc_field.append([2.0], 2) if "DPF_DOCKER" not in os.environ.keys(): print(in_process_field, type(in_process_field._server), in_process_field._server) print(grpc_field, type(grpc_field._server), grpc_field._server) print(legacy_grpc_field, type(legacy_grpc_field._server), legacy_grpc_field._server) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Nodal Unit: 2 entities Data: 1 components and 2 elementary data IDs data ------------ ---------- 1 1.000000e+00 2 2.000000e+00 DPF Server: {'server_ip': '', 'server_port': None, 'server_process_id': 2428, 'server_version': '7.0', 'os': 'nt'} DPF Field Location: Nodal Unit: 2 entities Data: 1 components and 2 elementary data IDs data ------------ ---------- 1 1.000000e+00 2 2.000000e+00 DPF Server: {'server_ip': '127.0.0.1', 'server_port': 50056, 'server_process_id': 1968, 'server_version': '7.0', 'os': 'nt'} DPF Field Location: Nodal Unit: 2 entities Data: 1 components and 2 elementary data IDs data ------------ ---------- 1 1.000000e+00 2 2.000000e+00 DPF Server: {'server_ip': '127.0.0.1', 'server_port': 50057, 'server_process_id': 1268, 'server_version': '7.0', 'os': 'nt'} .. GENERATED FROM PYTHON SOURCE LINES 86-90 Choose default configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Once a default configuration is chosen, a server of the chosen type is automatically started when a DPF object is created: .. GENERATED FROM PYTHON SOURCE LINES 90-101 .. code-block:: default initial_config = dpf.SERVER_CONFIGURATION dpf.SERVER_CONFIGURATION = dpf.AvailableServerConfigs.GrpcServer grpc_field = dpf.fields_factory.create_scalar_field(2) grpc_field.append([1.0], 1) grpc_field.append([2.0], 2) print(grpc_field, type(grpc_field._server), grpc_field._server) # Go back to default config: dpf.SERVER_CONFIGURATION = initial_config .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Nodal Unit: 2 entities Data: 1 components and 2 elementary data IDs data ------------ ---------- 1 1.000000e+00 2 2.000000e+00 DPF Server: {'server_ip': '127.0.0.1', 'server_port': 50055, 'server_process_id': 6136, 'server_version': '7.0', 'os': 'nt'} .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 30.347 seconds) .. _sphx_glr_download_examples_00-basic_11-server_types.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 11-server_types.py <11-server_types.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 11-server_types.ipynb <11-server_types.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_