Skip to content

Commit

Permalink
FEATURE: Checking for port used before starting server session
Browse files Browse the repository at this point in the history
  • Loading branch information
chenchienjacklin committed Apr 24, 2024
1 parent fc72907 commit 6a7f698
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/ansys/edb/core/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,7 @@ def launch_session(ansys_em_root, port_num=None):
ip_address = None # remote launch is not supported yet

try:
MOD.current_session = _Session(ip_address, port_num, ansys_em_root)
MOD.current_session.connect()
__ensure_session(ansys_em_root, port_num, ip_address)
return MOD.current_session
except Exception as e: # noqa
if MOD.current_session is not None:
Expand Down Expand Up @@ -504,8 +503,7 @@ def session(ansys_em_root, port_num, ip_address=None):
>>> # program goes here
"""
try:
MOD.current_session = _Session(ip_address, port_num, ansys_em_root)
MOD.current_session.connect()
__ensure_session(ip_address, port_num, ansys_em_root)
yield
except EDBSessionException:
raise
Expand Down Expand Up @@ -553,3 +551,23 @@ def get_variable_server_stub():
VariableServerServiceStub
"""
return StubAccessor(StubType.variable_server).__get__()


def __ensure_session(ansys_em_root, port_num, ip_address):
"""Check for a running local session and create one if it doesn't exist.
Parameters
----------
ansys_em_root : str
Directory where the ``EDB_RPC_Server.exe`` file is installed.
port_num : int
Port number to listen on.
ip_address : str, default: None
IP address where the server executable file is running.
"""
if MOD.current_session is not None:
if MOD.current_session.port_num != port_num:
raise EDBSessionException(ErrorCode.STARTUP_MULTI_SESSIONS)
else:
MOD.current_session = _Session(ip_address, port_num, ansys_em_root)
MOD.current_session.connect()

0 comments on commit 6a7f698

Please sign in to comment.