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.

Import Required Modules#

Import the required modules.

from ansys.dpf import core as dpf
from ansys.dpf.core import server_context

Start a Server with Entry Context#

Start a local insecure gRPC server in Entry context using AvailableServerContexts. Passing as_global=False keeps the global server unchanged so subsequent cells are not affected.

# 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)
Server Context of type LicensingContextType.entry with no xml path

Upgrade the Server Context to Premium#

Once a server is running in Entry context, it can be upgraded to Premium using apply_context.

Note

Downgrading a server from Premium back to Entry is not supported.

# 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()
Server Context of type LicensingContextType.premium with no xml path

Change the Default Server Context#

The default context for all new servers is Premium. You can change it at runtime using 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.

# 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)
Server Context of type LicensingContextType.entry with no xml path
Server Context of type LicensingContextType.premium with no xml path

Total running time of the script: (0 minutes 0.678 seconds)

Gallery generated by Sphinx-Gallery