Skip to content
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

fix: timeout for file checking #3642

Merged
merged 12 commits into from
Jan 8, 2025
1 change: 1 addition & 0 deletions src/ansys/mapdl/core/mapdl_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2919,6 +2919,7 @@ def _check_mapdl_os(self):
self._platform = "windows"
else: # pragma: no cover
raise MapdlRuntimeError("Unknown platform: {}".format(platform))
self.logger.debug(f"MAPDL is running on {self._platform} OS.")

def _check_on_docker(self):
"""Check if MAPDL is running on docker."""
Expand Down
73 changes: 51 additions & 22 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@

SESSION_ID_NAME = "__PYMAPDL_SESSION_ID__"

DEFAULT_TIME_STEP_STREAM = None
DEFAULT_TIME_STEP_STREAM_NT = 500
DEFAULT_TIME_STEP_STREAM_POSIX = 100

# Retry policy for gRPC calls.
SERVICE_DEFAULT_CONFIG = {
# see https://github.com/grpc/proposal/blob/master/A6-client-retries.md#retry-policy-capabilities
Expand Down Expand Up @@ -1069,7 +1073,8 @@ def _send_command(self, cmd: str, mute: bool = False) -> Optional[str]:
def _send_command_stream(self, cmd, verbose=False) -> str:
"""Send a command and expect a streaming response"""
request = pb_types.CmdRequest(command=cmd)
metadata = [("time_step_stream", "100")]
time_step = self._get_time_step_stream()
metadata = [("time_step_stream", str(time_step))]
stream = self._stub.SendCommandS(request, metadata=metadata)
response = []
for item in stream:
Expand Down Expand Up @@ -1775,13 +1780,14 @@ def input(
execution time.

Due to stability issues, the default time_step_stream is
dependent on verbosity. The defaults are:
dependent on the OS MAPDL is running on. The defaults are:

- ``verbose=True``: ``time_step_stream=500``
- ``verbose=False``: ``time_step_stream=50``
- Windows: ``time_step_stream=500``
- Linux: ``time_step_stream=100``

These defaults will be ignored if ``time_step_stream`` is
manually set.
manually set. See the *Examples* section to learn how to change
the default value globally.

orig_cmd : str, optional
Original command. There are some cases, were input is
Expand Down Expand Up @@ -1831,6 +1837,11 @@ def input(
>>> with mapdl.non_interactive:
mapdl.run("/input,inputtrigger,inp") # This inputs 'myinput.inp'

You can also change them globably using:

>>> from ansys.mapdl.core import mapdl_grpc
>>> mapdl_grpc.DEFAULT_TIME_STEP_STREAM=100 # in milliseconds

"""
# Checking compatibility
# Checking the user is not reusing old api:
Expand Down Expand Up @@ -1911,18 +1922,14 @@ def input(
# are unclear
filename = self._get_file_path(fname, progress_bar)

if time_step_stream is not None:
if time_step_stream <= 0:
raise ValueError("``time_step_stream`` must be greater than 0``")
time_step_stream = self._get_time_step_stream(time_step_stream)

if verbose:
if time_step_stream is None:
time_step_stream = 500
metadata = [
("time_step_stream", str(time_step_stream)),
("chunk_size", str(chunk_size)),
]
metadata = [
("time_step_stream", str(time_step_stream)),
("chunk_size", str(chunk_size)),
]

if verbose:
request = pb_types.InputFileRequest(filename=filename)
strouts = self._stub.InputFileS(request, metadata=metadata)
responses = []
Expand All @@ -1934,13 +1941,8 @@ def input(
response = "\n".join(responses)
return response.strip()

# otherwise, not verbose
if time_step_stream is None:
time_step_stream = 50
metadata = [
("time_step_stream", str(time_step_stream)),
("chunk_size", str(chunk_size)),
]
##
# Otherwise, not verbose

# since we can't directly run /INPUT, we have to write a
# temporary input file that tells MAPDL to read the input
Expand Down Expand Up @@ -2014,6 +2016,33 @@ def input(

return output

def _get_time_step_stream(
self, time_step: Optional[Union[int, float]] = None
) -> str:
"""Return the time step for checking if MAPDL is done writing the
output to the file which later will be returned as response
"""
if time_step is None:
if DEFAULT_TIME_STEP_STREAM is not None:
time_step = DEFAULT_TIME_STEP_STREAM
elif self.platform == "windows":
time_step = DEFAULT_TIME_STEP_STREAM_NT
elif self.platform == "linux":
time_step = DEFAULT_TIME_STEP_STREAM_POSIX
else:
raise ValueError(
f"The MAPDL platform ('{self.platform}') is not recognaised."
)

else:
if time_step_stream <= 0:
raise ValueError(
"``time_step_stream`` argument must be greater than 0``"
)

self.logger.debug(f"The time_step_stream argument is set to: {time_step}")
return time_step

def _get_file_path(self, fname: str, progress_bar: bool = False) -> str:
"""Find files in the Python and MAPDL working directories.

Expand Down
Loading