.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\04-advanced\13-manage_licensing.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_04-advanced_13-manage_licensing.py: .. _ref_manage_licensing: Manage the DPF licensing logic using the server context ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This example shows how to manage the licensing logic of a DPF server using a `ServerContext`. You can prevent DPF from checking licenses out and blocking increments by using the **Entry** context. .. warning:: You cannot start a new ``InProcess`` server, as starting an ``InProcess`` server means linking the DPF binaries to your current Python process. If your local ``InProcess`` server is already set to **Premium**, you cannot set it back to **Entry**. Since ``InProcess`` is the default server type, put the commands to set the **Entry** server context at the start of your script. .. note:: This example requires DPF 6.1 (Ansys 2023R2) or above. For more information, see :ref:`ref_compatibility`. .. GENERATED FROM PYTHON SOURCE LINES 24-28 .. code-block:: Python # Import necessary modules from ansys.dpf import core as dpf from ansys.dpf.core.core import errors .. GENERATED FROM PYTHON SOURCE LINES 29-30 Start a server as Entry to prevent using licensed operators .. GENERATED FROM PYTHON SOURCE LINES 30-41 .. code-block:: Python server = dpf.start_local_server( context=dpf.AvailableServerContexts.entry, config=dpf.AvailableServerConfigs.GrpcServer, as_global=False, ) # The context is shown as Entry print(server.context) # A server of type InProcess being linked to the current Python process, # if an InProcess server already exists as Premium, you cannot set it back as Entry. .. rst-class:: sphx-glr-script-out .. code-block:: none Server Context of type LicensingContextType.entry with no xml path .. GENERATED FROM PYTHON SOURCE LINES 42-43 Create a dummy Field .. GENERATED FROM PYTHON SOURCE LINES 43-53 .. code-block:: Python field = dpf.Field(server=server) field.append([0.0, 0.0, 0.0], 1) print(field) # Instantiate an Entry (not licensed) DPF operator op_entry = dpf.operators.math.add_constant(field=field, ponderation=2.0, server=server) # Instantiate a Premium (licensed) DPF operator op_premium = dpf.operators.filter.field_high_pass(field=field, threshold=0.0, server=server) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Nodal Unit: 1 entities Data: 3 components and 1 elementary data IDs data ------------ ---------- 1 0.000000e+00 0.000000e+00 0.000000e+00 .. GENERATED FROM PYTHON SOURCE LINES 54-55 Operators with the Entry context .. GENERATED FROM PYTHON SOURCE LINES 55-66 .. code-block:: Python # Using unlicensed DPF operators is possible out = op_entry.eval() print(out) # While using license ones is blocked, raising an error try: op_premium.eval() except errors.DPFServerException as e: print(e) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Nodal Unit: 1 entities Data: 3 components and 1 elementary data IDs data ------------ ---------- 1 2.000000e+00 2.000000e+00 2.000000e+00 a data processing core error error ocurred: core::field::high_pass:3<-DPF issue due to licensing context: execution stopped. Apply Premium context to unlock this capability. .. GENERATED FROM PYTHON SOURCE LINES 67-68 Operators with the Premium context .. GENERATED FROM PYTHON SOURCE LINES 68-78 .. code-block:: Python # Set the default server context as Premium for new servers dpf.set_default_server_context(dpf.AvailableServerContexts.premium) # or in our case, apply the Premium context to the current server server.apply_context(dpf.AvailableServerContexts.premium) # Licensed operators can now check a license out and run out = op_premium.eval() print(out) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Nodal Unit: 0 entities Data: 3 components and 0 elementary data .. GENERATED FROM PYTHON SOURCE LINES 79-82 When Premium, using a LicenseContextManaged allows you to control your interaction with a license It gives direct control over when the license check-out and check-in occur, as well as which license increment is used, and for what maximum duration. .. GENERATED FROM PYTHON SOURCE LINES 82-88 .. code-block:: Python # Use the LicenseContextManager to block a specific increment for a limited duration with dpf.LicenseContextManager(increment_name="preppost", license_timeout_in_seconds=1.0) as lic: # Instantiate the licensed operator out = op_premium.eval() print(out) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Field Location: Nodal Unit: 0 entities Data: 3 components and 0 elementary data .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 9.412 seconds) .. _sphx_glr_download_examples_04-advanced_13-manage_licensing.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 13-manage_licensing.ipynb <13-manage_licensing.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 13-manage_licensing.py <13-manage_licensing.py>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_