Skip to content
4 changes: 4 additions & 0 deletions src/ansys/dpf/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@ def initialize_with_context(self, context):
int(context.licensing_context_type), context.xml_path
)

def initialize(self):
"""Initialize a DPF server without a context."""
self._api.data_processing_initialization()

@version_requires("6.0")
def release_dpf(self):
"""Clears the available Operators and Releases licenses when necessary.
Expand Down
2 changes: 2 additions & 0 deletions src/ansys/dpf/core/server_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@


class LicensingContextType(Enum):
none = 5
premium = 1
"""Checks if at least one license increment exists
and allows operators to block an increment."""
Expand Down Expand Up @@ -207,6 +208,7 @@ def __ne__(self, other):
class AvailableServerContexts:
"""Defines available server contexts."""

no_context = ServerContext(LicensingContextType.none, "")
pre_defined_environment = ServerContext(0)
"""DataProcessingCore.xml that is next to DataProcessingCore.dll/libDataProcessingCore.so will
be taken"""
Expand Down
113 changes: 65 additions & 48 deletions src/ansys/dpf/core/server_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Contains the different kinds of
servers available for the factory.
"""
from __future__ import annotations
import abc
import io
import os
Expand All @@ -15,7 +16,8 @@
import traceback
from threading import Thread, Lock
from abc import ABC
from ctypes import *
import ctypes
from typing import TYPE_CHECKING, Union

import psutil

Expand All @@ -26,6 +28,9 @@
from ansys.dpf.core import server_context
from ansys.dpf.gate import load_api, data_processing_grpcapi

if TYPE_CHECKING:
from ansys.dpf.core.server_factory import DockerConfig

import logging

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -671,17 +676,17 @@ class GrpcServer(CServer):

def __init__(
self,
ansys_path=None,
ip=LOCALHOST,
port=DPF_DEFAULT_PORT,
timeout=10,
as_global=True,
load_operators=True,
launch_server=True,
docker_config=RUNNING_DOCKER,
use_pypim=True,
num_connection_tryouts=3,
context=server_context.SERVER_CONTEXT,
ansys_path: Union[str, None] = None,
ip: str = LOCALHOST,
port: str = DPF_DEFAULT_PORT,
timeout: float = 10.,
as_global: bool = True,
load_operators: bool = True,
launch_server: bool = True,
docker_config: DockerConfig = RUNNING_DOCKER,
use_pypim: bool = True,
num_connection_tryouts: int = 3,
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
):
# Load DPFClientAPI
from ansys.dpf.core.misc import is_pypim_configured
Expand Down Expand Up @@ -733,11 +738,15 @@ def __init__(
self.live = True
self._create_shutdown_funcs()
self._check_first_call(num_connection_tryouts)
try:
self._base_service.initialize_with_context(context)
if context != core.AvailableServerContexts.no_context:
try:
self._base_service.initialize_with_context(context)
self._context = context
except errors.DpfVersionNotSupported:
pass
else:
self._base_service.initialize()
self._context = context
except errors.DpfVersionNotSupported:
pass
self.set_as_global(as_global=as_global)

def _check_first_call(self, num_connection_tryouts):
Expand Down Expand Up @@ -892,11 +901,11 @@ class InProcessServer(CServer):

def __init__(
self,
ansys_path=None,
as_global=True,
load_operators=True,
timeout=None,
context=server_context.SERVER_CONTEXT,
ansys_path: Union[str, None] = None,
as_global: bool = True,
load_operators: bool = True,
timeout: None = None,
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
):
# Load DPFClientAPI
super().__init__(ansys_path=ansys_path, load_operators=load_operators)
Expand All @@ -914,14 +923,18 @@ def __init__(
f"Unable to locate the following file: {path}"
)
raise e
try:
self.apply_context(context)
except errors.DpfVersionNotSupported:
self._base_service.initialize_with_context(
server_context.AvailableServerContexts.premium
)
self._context = server_context.AvailableServerContexts.premium
pass
if context != core.AvailableServerContexts.no_context:
try:
self.apply_context(context)
except errors.DpfVersionNotSupported:
self._base_service.initialize_with_context(
server_context.AvailableServerContexts.premium
)
self._context = server_context.AvailableServerContexts.premium
pass
else:
self._base_service.initialize()
self._context = context
self.set_as_global(as_global=as_global)
# Update the python os.environment
if not os.name == "posix":
Expand Down Expand Up @@ -974,19 +987,20 @@ def config(self):
def get_system_path() -> str:
"""Return the current PATH environment variable value of the system."""
if not os.name == "posix":
windll.kernel32.GetEnvironmentVariableA.argtypes = (c_char_p, c_char_p, c_int)
windll.kernel32.GetEnvironmentVariableA.restype = c_int
ctypes.windll.kernel32.GetEnvironmentVariableA.argtypes = (
ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int
)
ctypes.windll.kernel32.GetEnvironmentVariableA.restype = ctypes.c_int
name = "PATH"
b_name = name.encode("utf-8")
size = 32767
buffer = create_string_buffer(b"", size)
_ = windll.kernel32.GetEnvironmentVariableA(b_name, buffer, size)
buffer = ctypes.create_string_buffer(b"", size)
_ = ctypes.windll.kernel32.GetEnvironmentVariableA(b_name, buffer, size)
return buffer.value.decode("utf-8")
else:
return sys.path



class LegacyGrpcServer(BaseServer):
"""Provides an instance of the DPF server using InProcess gRPC.
Kept for backward-compatibility with dpf servers <0.5.0.
Expand Down Expand Up @@ -1023,16 +1037,16 @@ class LegacyGrpcServer(BaseServer):

def __init__(
self,
ansys_path=None,
ip=LOCALHOST,
port=DPF_DEFAULT_PORT,
timeout=5,
as_global=True,
load_operators=True,
launch_server=True,
docker_config=RUNNING_DOCKER,
use_pypim=True,
context=server_context.SERVER_CONTEXT,
ansys_path: Union[str, None] = None,
ip: str = LOCALHOST,
port: str = DPF_DEFAULT_PORT,
timeout: float = 5.,
as_global: bool = True,
load_operators: bool = True,
launch_server: bool = True,
docker_config: DockerConfig = RUNNING_DOCKER,
use_pypim: bool = True,
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
):
"""Start the DPF server."""
# Use ansys.grpc.dpf
Expand Down Expand Up @@ -1097,11 +1111,14 @@ def __init__(
self._create_shutdown_funcs()

check_ansys_grpc_dpf_version(self, timeout)
try:
self._base_service.initialize_with_context(context)
if context != core.AvailableServerContexts.no_context:
try:
self._base_service.initialize_with_context(context)
self._context = context
except errors.DpfVersionNotSupported:
pass
else:
self._context = context
except errors.DpfVersionNotSupported:
pass
self.set_as_global(as_global=as_global)

def _create_shutdown_funcs(self):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,17 @@ def test_context_environment_variable(reset_context_environment_variable):
continue


def test_server_without_context(remote_config_server_type):
"""Tests starting a server without a no_context given."""
server = dpf.core.start_local_server(
as_global=True,
config=remote_config_server_type,
context=dpf.core.AvailableServerContexts.no_context
)
print(server.context)
assert server.context == dpf.core.AvailableServerContexts.no_context


@pytest.mark.order("last")
@pytest.mark.skipif(
running_docker or not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_5_0,
Expand Down