.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials\licensing\server_context.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_tutorials_licensing_server_context.py: .. _ref_tutorials_server_context: Server Context ============== Control licensing behavior by configuring the |ServerContext| on a DPF server. The |ServerContext| drives whether DPF capabilities requiring a license checkout are allowed. Two licensing context types are available: - **Premium**: The default context, which allows DPF to perform license checkouts and makes all licensed operators available. - **Entry**: A restricted context that performs no license checkouts. Licensed operators are unavailable in this context. This tutorial shows how to: - Start a server with the **Entry** context. - Upgrade a server from **Entry** to **Premium** at runtime. - Change the default context for all new servers. .. note:: The **Entry** server context is available starting with DPF server version 6.0 (Ansys 2023 R2). With earlier server versions, **Premium** is the only available context. .. GENERATED FROM PYTHON SOURCE LINES 54-58 Import Required Modules ----------------------- Import the required modules. .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: Python from ansys.dpf import core as dpf from ansys.dpf.core import server_context .. GENERATED FROM PYTHON SOURCE LINES 63-70 Start a Server with Entry Context ---------------------------------- Start a local insecure gRPC server in **Entry** context using :class:`AvailableServerContexts `. Passing ``as_global=False`` keeps the global server unchanged so subsequent cells are not affected. .. GENERATED FROM PYTHON SOURCE LINES 70-81 .. code-block:: Python # Start a local insecure gRPC server with the Entry licensing context entry_server = dpf.start_local_server( context=dpf.AvailableServerContexts.entry, as_global=False, config=dpf.AvailableServerConfigs.InsecureGrpcServer, ) # Display the server context — shows LicensingContextType.entry print(entry_server.context) .. rst-class:: sphx-glr-script-out .. code-block:: none Server Context of type LicensingContextType.entry with no xml path .. GENERATED FROM PYTHON SOURCE LINES 82-92 Upgrade the Server Context to Premium -------------------------------------- Once a server is running in **Entry** context, it can be upgraded to **Premium** using :meth:`apply_context `. .. note:: Downgrading a server from **Premium** back to **Entry** is not supported. .. GENERATED FROM PYTHON SOURCE LINES 92-102 .. code-block:: Python # Apply the Premium context to the Entry server entry_server.apply_context(dpf.AvailableServerContexts.premium) # Display the server context — now shows LicensingContextType.premium print(entry_server.context) # Shut down the server to free resources entry_server.shutdown() .. rst-class:: sphx-glr-script-out .. code-block:: none Server Context of type LicensingContextType.premium with no xml path .. GENERATED FROM PYTHON SOURCE LINES 103-120 Change the Default Server Context ---------------------------------- The default context for all new servers is **Premium**. You can change it at runtime using :func:`set_default_server_context `. It can also be set before starting your Python process via the ``ANSYS_DPF_SERVER_CONTEXT`` environment variable (accepted values: ``ENTRY``, ``PREMIUM``). .. warning:: Because an ``InProcess`` server links DPF binaries to the current Python process, a second ``InProcess`` server cannot be started. If your local ``InProcess`` server is already **Premium**, it cannot be set back to **Entry**. Set the desired context at the very start of your script, before the first server is created. .. GENERATED FROM PYTHON SOURCE LINES 120-132 .. code-block:: Python # Set Entry as the default context for new servers dpf.set_default_server_context(dpf.AvailableServerContexts.entry) # Display the new global default context print(server_context.SERVER_CONTEXT) # Restore Premium as the default context dpf.set_default_server_context(dpf.AvailableServerContexts.premium) # Display the restored default context print(server_context.SERVER_CONTEXT) .. rst-class:: sphx-glr-script-out .. code-block:: none Server Context of type LicensingContextType.entry with no xml path Server Context of type LicensingContextType.premium with no xml path .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.678 seconds) .. _sphx_glr_download_tutorials_licensing_server_context.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: server_context.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: server_context.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: server_context.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_