Skip to content

Commit

Permalink
fix: timeout for file checking (#3642)
Browse files Browse the repository at this point in the history
* fix: increase timeout for checking output file

* feat: implement global defaults OS dependent.

* feat: adding logging

* fix: increase timeout for checking output file

* feat: implement global defaults OS dependent.

* feat: adding logging

* chore: adding changelog file 3642.fixed.md [dependabot-skip]

* fix: wrong variable

* tests: adding tests

* ci: not hiding log

---------

Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
germa89 and pyansys-ci-bot authored Jan 8, 2025
1 parent a4354c7 commit a7d4359
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ jobs:
python-package-name: ${{ env.PACKAGE_NAME }}
dev-mode: ${{ github.ref != 'refs/heads/main' }}
upload-reports: True
hide-log: false


docs-build:
name: "Build documentation"
Expand Down
1 change: 1 addition & 0 deletions doc/changelog.d/3642.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix: timeout for file checking
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
71 changes: 49 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,31 @@ 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 <= 0:
raise ValueError("``time_step`` argument must be greater than 0``")

self.logger.debug(f"The time_step 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
30 changes: 30 additions & 0 deletions tests/test_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import re
import shutil
import sys
from unittest.mock import patch

import grpc
import pytest
Expand Down Expand Up @@ -682,3 +683,32 @@ def _null_close_process():
mapdl.prep7(mapdl)

mapdl._exited = False # Restoring


@pytest.mark.parametrize("platform", ["linux", "windows", "error"])
def test__get_time_step_stream(mapdl, platform):
with patch("ansys.mapdl.core.mapdl_grpc.MapdlGrpc.platform", platform):
from ansys.mapdl.core import mapdl_grpc

if platform == "linux":
DEFAULT_TIME_STEP_STREAM = mapdl_grpc.DEFAULT_TIME_STEP_STREAM_POSIX
elif platform == "windows":
DEFAULT_TIME_STEP_STREAM = mapdl_grpc.DEFAULT_TIME_STEP_STREAM_NT
else:
with pytest.raises(ValueError, match="The MAPDL platform"):
mapdl._get_time_step_stream()

return # Early exit

assert DEFAULT_TIME_STEP_STREAM == mapdl._get_time_step_stream()

mapdl_grpc.DEFAULT_TIME_STEP_STREAM = 200
assert mapdl_grpc.DEFAULT_TIME_STEP_STREAM == mapdl._get_time_step_stream()
mapdl_grpc.DEFAULT_TIME_STEP_STREAM = None

assert 700 == mapdl._get_time_step_stream(700)

with pytest.raises(
ValueError, match="``time_step`` argument must be greater than 0``"
):
mapdl._get_time_step_stream(-700)

0 comments on commit a7d4359

Please sign in to comment.