44Contains the different kinds of
55servers available for the factory.
66"""
7+ from __future__ import annotations
78import abc
89import io
910import os
1516import traceback
1617from threading import Thread , Lock
1718from abc import ABC
18- from ctypes import *
19+ import ctypes
20+ from typing import TYPE_CHECKING , Union
1921
2022import psutil
2123
2628from ansys .dpf .core import server_context
2729from ansys .dpf .gate import load_api , data_processing_grpcapi
2830
31+ if TYPE_CHECKING :
32+ from ansys .dpf .core .server_factory import DockerConfig
33+
2934import logging
3035
3136LOG = 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):
974989def 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-
9901006class 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 ):
0 commit comments