Skip to content

Commit 08d6203

Browse files
authored
Add a no_context ServerContext (#1613)
* Improve typehinting Signed-off-by: paul.profizi <[email protected]> * Add BaseService.initialize() Signed-off-by: paul.profizi <[email protected]> * Initialize servers with no context Signed-off-by: paul.profizi <[email protected]> * Add a no_context ServerContext Signed-off-by: paul.profizi <[email protected]> * Add a test Signed-off-by: paul.profizi <[email protected]> * Fix retro and case where SERVER_CONTEXT is indeed None (Docker) Signed-off-by: paul.profizi <[email protected]> * Try fixing LicensingContextType.same_licensing_context for LicensingContextType.none Signed-off-by: paul.profizi <[email protected]> * Fix test_server_without_context Signed-off-by: paul.profizi <[email protected]> * Fix test_server_without_context Signed-off-by: paul.profizi <[email protected]> * Initialize on client if server has client Signed-off-by: paul.profizi <[email protected]> * Skip test <232 Signed-off-by: paul.profizi <[email protected]> * Skip test <232 Signed-off-by: paul.profizi <[email protected]> --------- Signed-off-by: paul.profizi <[email protected]>
1 parent 5f4db80 commit 08d6203

File tree

4 files changed

+99
-50
lines changed

4 files changed

+99
-50
lines changed

src/ansys/dpf/core/core.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,15 @@ def initialize_with_context(self, context):
499499
int(context.licensing_context_type), context.xml_path
500500
)
501501

502+
def initialize(self):
503+
"""Initialize a DPF server without a context."""
504+
if self._server().has_client():
505+
self._api.data_processing_initialization_on_client(
506+
self._server().client
507+
)
508+
else:
509+
self._api.data_processing_initialization()
510+
502511
@version_requires("6.0")
503512
def release_dpf(self):
504513
"""Clears the available Operators and Releases licenses when necessary.

src/ansys/dpf/core/server_context.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222

2323
class LicensingContextType(Enum):
24+
none = 5
2425
premium = 1
2526
"""Checks if at least one license increment exists
2627
and allows operators to block an increment."""
@@ -33,6 +34,9 @@ def __int__(self):
3334

3435
@staticmethod
3536
def same_licensing_context(first, second):
37+
if ((first == LicensingContextType.none and second != LicensingContextType.none)
38+
or (first != LicensingContextType.none and second == LicensingContextType.none)):
39+
return False
3640
if int(first) == int(LicensingContextType.entry) and int(second) != int(
3741
LicensingContextType.entry
3842
):
@@ -207,6 +211,7 @@ def __ne__(self, other):
207211
class AvailableServerContexts:
208212
"""Defines available server contexts."""
209213

214+
no_context = ServerContext(LicensingContextType.none, "")
210215
pre_defined_environment = ServerContext(0)
211216
"""DataProcessingCore.xml that is next to DataProcessingCore.dll/libDataProcessingCore.so will
212217
be taken"""

src/ansys/dpf/core/server_types.py

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Contains the different kinds of
55
servers available for the factory.
66
"""
7+
from __future__ import annotations
78
import abc
89
import io
910
import os
@@ -15,7 +16,8 @@
1516
import traceback
1617
from threading import Thread, Lock
1718
from abc import ABC
18-
from ctypes import *
19+
import ctypes
20+
from typing import TYPE_CHECKING, Union
1921

2022
import psutil
2123

@@ -26,6 +28,9 @@
2628
from ansys.dpf.core import server_context
2729
from ansys.dpf.gate import load_api, data_processing_grpcapi
2830

31+
if TYPE_CHECKING:
32+
from ansys.dpf.core.server_factory import DockerConfig
33+
2934
import logging
3035

3136
LOG = logging.getLogger(__name__)
@@ -671,17 +676,17 @@ class GrpcServer(CServer):
671676

672677
def __init__(
673678
self,
674-
ansys_path=None,
675-
ip=LOCALHOST,
676-
port=DPF_DEFAULT_PORT,
677-
timeout=10,
678-
as_global=True,
679-
load_operators=True,
680-
launch_server=True,
681-
docker_config=RUNNING_DOCKER,
682-
use_pypim=True,
683-
num_connection_tryouts=3,
684-
context=server_context.SERVER_CONTEXT,
679+
ansys_path: Union[str, None] = None,
680+
ip: str = LOCALHOST,
681+
port: str = DPF_DEFAULT_PORT,
682+
timeout: float = 10.,
683+
as_global: bool = True,
684+
load_operators: bool = True,
685+
launch_server: bool = True,
686+
docker_config: DockerConfig = RUNNING_DOCKER,
687+
use_pypim: bool = True,
688+
num_connection_tryouts: int = 3,
689+
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
685690
):
686691
# Load DPFClientAPI
687692
from ansys.dpf.core.misc import is_pypim_configured
@@ -733,11 +738,16 @@ def __init__(
733738
self.live = True
734739
self._create_shutdown_funcs()
735740
self._check_first_call(num_connection_tryouts)
736-
try:
737-
self._base_service.initialize_with_context(context)
738-
self._context = context
739-
except errors.DpfVersionNotSupported:
740-
pass
741+
if context:
742+
if context == core.AvailableServerContexts.no_context:
743+
self._base_service.initialize()
744+
self._context = context
745+
else:
746+
try:
747+
self._base_service.initialize_with_context(context)
748+
self._context = context
749+
except errors.DpfVersionNotSupported:
750+
pass
741751
self.set_as_global(as_global=as_global)
742752

743753
def _check_first_call(self, num_connection_tryouts):
@@ -892,11 +902,11 @@ class InProcessServer(CServer):
892902

893903
def __init__(
894904
self,
895-
ansys_path=None,
896-
as_global=True,
897-
load_operators=True,
898-
timeout=None,
899-
context=server_context.SERVER_CONTEXT,
905+
ansys_path: Union[str, None] = None,
906+
as_global: bool = True,
907+
load_operators: bool = True,
908+
timeout: None = None,
909+
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
900910
):
901911
# Load DPFClientAPI
902912
super().__init__(ansys_path=ansys_path, load_operators=load_operators)
@@ -914,14 +924,19 @@ def __init__(
914924
f"Unable to locate the following file: {path}"
915925
)
916926
raise e
917-
try:
918-
self.apply_context(context)
919-
except errors.DpfVersionNotSupported:
920-
self._base_service.initialize_with_context(
921-
server_context.AvailableServerContexts.premium
922-
)
923-
self._context = server_context.AvailableServerContexts.premium
924-
pass
927+
if context:
928+
if context == core.AvailableServerContexts.no_context:
929+
self._base_service.initialize()
930+
self._context = context
931+
else:
932+
try:
933+
self.apply_context(context)
934+
except errors.DpfVersionNotSupported:
935+
self._base_service.initialize_with_context(
936+
server_context.AvailableServerContexts.premium
937+
)
938+
self._context = server_context.AvailableServerContexts.premium
939+
pass
925940
self.set_as_global(as_global=as_global)
926941
# Update the python os.environment
927942
if not os.name == "posix":
@@ -974,19 +989,20 @@ def config(self):
974989
def get_system_path() -> str:
975990
"""Return the current PATH environment variable value of the system."""
976991
if not os.name == "posix":
977-
windll.kernel32.GetEnvironmentVariableA.argtypes = (c_char_p, c_char_p, c_int)
978-
windll.kernel32.GetEnvironmentVariableA.restype = c_int
992+
ctypes.windll.kernel32.GetEnvironmentVariableA.argtypes = (
993+
ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int
994+
)
995+
ctypes.windll.kernel32.GetEnvironmentVariableA.restype = ctypes.c_int
979996
name = "PATH"
980997
b_name = name.encode("utf-8")
981998
size = 32767
982-
buffer = create_string_buffer(b"", size)
983-
_ = windll.kernel32.GetEnvironmentVariableA(b_name, buffer, size)
999+
buffer = ctypes.create_string_buffer(b"", size)
1000+
_ = ctypes.windll.kernel32.GetEnvironmentVariableA(b_name, buffer, size)
9841001
return buffer.value.decode("utf-8")
9851002
else:
9861003
return sys.path
9871004

9881005

989-
9901006
class LegacyGrpcServer(BaseServer):
9911007
"""Provides an instance of the DPF server using InProcess gRPC.
9921008
Kept for backward-compatibility with dpf servers <0.5.0.
@@ -1023,16 +1039,16 @@ class LegacyGrpcServer(BaseServer):
10231039

10241040
def __init__(
10251041
self,
1026-
ansys_path=None,
1027-
ip=LOCALHOST,
1028-
port=DPF_DEFAULT_PORT,
1029-
timeout=5,
1030-
as_global=True,
1031-
load_operators=True,
1032-
launch_server=True,
1033-
docker_config=RUNNING_DOCKER,
1034-
use_pypim=True,
1035-
context=server_context.SERVER_CONTEXT,
1042+
ansys_path: Union[str, None] = None,
1043+
ip: str = LOCALHOST,
1044+
port: str = DPF_DEFAULT_PORT,
1045+
timeout: float = 5.,
1046+
as_global: bool = True,
1047+
load_operators: bool = True,
1048+
launch_server: bool = True,
1049+
docker_config: DockerConfig = RUNNING_DOCKER,
1050+
use_pypim: bool = True,
1051+
context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT,
10361052
):
10371053
"""Start the DPF server."""
10381054
# Use ansys.grpc.dpf
@@ -1097,11 +1113,15 @@ def __init__(
10971113
self._create_shutdown_funcs()
10981114

10991115
check_ansys_grpc_dpf_version(self, timeout)
1100-
try:
1101-
self._base_service.initialize_with_context(context)
1102-
self._context = context
1103-
except errors.DpfVersionNotSupported:
1104-
pass
1116+
if context:
1117+
if context == core.AvailableServerContexts.no_context:
1118+
self._context = context
1119+
else:
1120+
try:
1121+
self._base_service.initialize_with_context(context)
1122+
self._context = context
1123+
except errors.DpfVersionNotSupported:
1124+
pass
11051125
self.set_as_global(as_global=as_global)
11061126

11071127
def _create_shutdown_funcs(self):

tests/test_service.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,21 @@ def test_context_environment_variable(reset_context_environment_variable):
481481
continue
482482

483483

484+
@pytest.mark.skipif(
485+
not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_6_0,
486+
reason="Failures on Windows 231"
487+
)
488+
def test_server_without_context(remote_config_server_type):
489+
"""Tests starting a server without a no_context given."""
490+
server = dpf.core.start_local_server(
491+
as_global=False,
492+
config=remote_config_server_type,
493+
context=dpf.core.AvailableServerContexts.no_context
494+
)
495+
none_type = dpf.core.AvailableServerContexts.no_context.licensing_context_type
496+
assert server.context.licensing_context_type == none_type
497+
498+
484499
@pytest.mark.order("last")
485500
@pytest.mark.skipif(
486501
running_docker or not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_5_0,

0 commit comments

Comments
 (0)