Skip to content

feat: exposing a reconnection method #3933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/3933.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: exposing a reconnection method
45 changes: 33 additions & 12 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,18 +464,7 @@ def __init__(
if not self._mapdl_on_hpc and self._mapdl_process:
self._create_process_stds_queue()

try:
self._multi_connect(timeout=timeout)
except MapdlConnectionError as err: # pragma: no cover
self._post_mortem_checks()
self._log.debug(
"The error wasn't caught by the post-mortem checks.\nThe stdout is printed now:"
)
self._log.debug(self._stdout)

raise err # Raise original error if we couldn't catch it in post-mortem analysis
else:
self._log.debug("Connection established")
self.reconnect_to_mapdl()

# Avoiding muting when connecting to the session
# It might trigger some errors later on if not.
Expand Down Expand Up @@ -527,6 +516,38 @@ def _before_run(self, _command: str) -> None:
# For some reason the session hasn't been created
self._create_session()

def _connect_to_mapdl(self, timeout: int):
"""(Re)connect to an existing MAPDL instance"""
try:
self._multi_connect(timeout=timeout)
except MapdlConnectionError as err: # pragma: no cover
self._post_mortem_checks()
self._log.debug(
"The error wasn't caught by the post-mortem checks.\nThe stdout is printed now:"
)
self._log.debug(self._stdout)

raise err # Raise original error if we couldn't catch it in post-mortem analysis
else:
self._log.debug("Connection established")

def reconnect_to_mapdl(self, timeout: int = None):
"""Reconnect to an already instantiated MAPDL instance.

Re-establish an stopped or crashed gRPC connection with an already alive
MAPDL instance. This function does not relaunch the MAPDL instance.

Parameters
----------
timeout : int, optional
Timeout before raising an exception, by default None
"""

if timeout is None:
timeout = self._timeout

self._connect_to_mapdl(timeout)

def _create_process_stds_queue(self, process=None):
from ansys.mapdl.core.launcher import (
_create_queue_for_std, # Avoid circular import error
Expand Down
Loading