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 23, 2024
1 parent fc72907 commit 4665219
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/ansys/edb/core/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,9 @@ 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()
if not has_session(port_num):
MOD.current_session = _Session(ip_address, port_num, ansys_em_root)
MOD.current_session.connect()
return MOD.current_session
except Exception as e: # noqa
if MOD.current_session is not None:
Expand Down Expand Up @@ -504,8 +505,9 @@ 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()
if not has_session(port_num):
MOD.current_session = _Session(ip_address, port_num, ansys_em_root)
MOD.current_session.connect()
yield
except EDBSessionException:
raise
Expand Down Expand Up @@ -553,3 +555,23 @@ def get_variable_server_stub():
VariableServerServiceStub
"""
return StubAccessor(StubType.variable_server).__get__()


def has_session(port_num):
"""Check if there is a running local session to an EDB API server.
Parameters
----------
port_num : int
Port number to listen on.
Returns
-------
Bool
"""
if MOD.current_session is not None:
if MOD.current_session.port_num == port_num:
return True
else:
raise EDBSessionException(ErrorCode.STARTUP_MULTI_SESSIONS)
return False

0 comments on commit 4665219

Please sign in to comment.