@@ -469,8 +469,7 @@ def launch_session(ansys_em_root, port_num=None):
469
469
ip_address = None # remote launch is not supported yet
470
470
471
471
try :
472
- MOD .current_session = _Session (ip_address , port_num , ansys_em_root )
473
- MOD .current_session .connect ()
472
+ _ensure_session (ansys_em_root , port_num , ip_address )
474
473
return MOD .current_session
475
474
except Exception as e : # noqa
476
475
if MOD .current_session is not None :
@@ -504,8 +503,7 @@ def session(ansys_em_root, port_num, ip_address=None):
504
503
>>> # program goes here
505
504
"""
506
505
try :
507
- MOD .current_session = _Session (ip_address , port_num , ansys_em_root )
508
- MOD .current_session .connect ()
506
+ _ensure_session (ansys_em_root , port_num , ip_address )
509
507
yield
510
508
except EDBSessionException :
511
509
raise
@@ -553,3 +551,23 @@ def get_variable_server_stub():
553
551
VariableServerServiceStub
554
552
"""
555
553
return StubAccessor (StubType .variable_server ).__get__ ()
554
+
555
+
556
+ def _ensure_session (ansys_em_root , port_num , ip_address ):
557
+ """Check for a running local session and create one if it doesn't exist.
558
+
559
+ Parameters
560
+ ----------
561
+ ansys_em_root : str
562
+ Directory where the ``EDB_RPC_Server.exe`` file is installed.
563
+ port_num : int
564
+ Port number to listen on.
565
+ ip_address : str, default: None
566
+ IP address where the server executable file is running.
567
+ """
568
+ if MOD .current_session is not None :
569
+ if MOD .current_session .port_num != port_num :
570
+ raise EDBSessionException (ErrorCode .STARTUP_MULTI_SESSIONS )
571
+ else :
572
+ MOD .current_session = _Session (ip_address , port_num , ansys_em_root )
573
+ MOD .current_session .connect ()
0 commit comments