Skip to content
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3d40750
test: refactor check_stds and post_mortem_checks
germa89 Jan 24, 2025
7e3a301
test: backing off algorithm
germa89 Jan 24, 2025
8bfb76a
chore: adding changelog file 3703.added.md [dependabot-skip]
pyansys-ci-bot Jan 24, 2025
6f7585a
fix: codacity warnings
germa89 Jan 24, 2025
0687f6e
Merge remote-tracking branch 'origin/main' into test/testing-performance
germa89 Jan 24, 2025
877f803
feat: using get_value to obtain the n elements
germa89 Jan 24, 2025
41ad078
revert: revert "feat: using get_value to obtain the n elements"
germa89 Jan 24, 2025
e046259
feat: using get_value to obtain the n elements
germa89 Jan 24, 2025
1556747
revert: revert "feat: using get_value to obtain the n elements"
germa89 Jan 24, 2025
52d9e92
feat: using mapdl.exit when raising final error.
germa89 Jan 24, 2025
2b29e52
test: fix test by avoiding killing mapdl.
germa89 Jan 24, 2025
991b31a
chore: merge remote-tracking branch 'origin/main' into test/testing-p…
germa89 Jan 24, 2025
c006f61
chore: merge remote-tracking branch 'origin/main' into test/testing-p…
germa89 Jan 24, 2025
d5d2267
fix: test
germa89 Jan 24, 2025
5c081cb
fix: test
germa89 Jan 27, 2025
0e26425
feat: adding warnings when restarting MAPDL during testing
germa89 Jan 27, 2025
8238ea8
fix: test
germa89 Jan 27, 2025
88c55fd
feat: caching requires_package
germa89 Jan 27, 2025
e9ff8f6
test: adding more tests
germa89 Jan 27, 2025
a0d0fa4
chore: merge remote-tracking branch 'origin/main' into test/testing-p…
germa89 Jan 27, 2025
fe526ce
chore: adding changelog file 3705.added.md [dependabot-skip]
pyansys-ci-bot Jan 27, 2025
12c9429
Merge branch 'main' into test/testing-performance
germa89 Jan 27, 2025
a4aa6a3
chore: adding changelog file 3705.added.md [dependabot-skip]
pyansys-ci-bot Jan 27, 2025
5593e6b
fix: warnings import
germa89 Jan 27, 2025
6d13fe7
Merge branch 'test/testing-performance' of https://github.com/ansys/p…
germa89 Jan 27, 2025
198b650
feat: not reconnecting if MAPDL already exited
germa89 Jan 27, 2025
0c72b4c
test: adding tests
germa89 Jan 27, 2025
74232ee
chore: adding changelog file 3708.miscellaneous.md [dependabot-skip]
pyansys-ci-bot Jan 27, 2025
e831962
fix: tests
germa89 Jan 27, 2025
b1a9eaa
Merge remote-tracking branch 'origin/main' into feat/avoiding-reconne…
germa89 Jan 27, 2025
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/3705.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: speed up `requires_package` using caching
1 change: 1 addition & 0 deletions doc/changelog.d/3708.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: avoiding reconnecting if MAPDL exited already
32 changes: 17 additions & 15 deletions src/ansys/mapdl/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,27 +327,29 @@ def wrapper(*args, **kwargs):

except grpc.RpcError as error:
mapdl = retrieve_mapdl_from_args(args)

mapdl._log.debug("A gRPC error has been detected.")

i_attemps += 1
if i_attemps <= n_attempts:
if not mapdl.exited:
i_attemps += 1
if i_attemps <= n_attempts:

wait = (
initial_backoff * multiplier_backoff**i_attemps
) # Exponential backoff
wait = (
initial_backoff * multiplier_backoff**i_attemps
) # Exponential backoff

# reconnect
mapdl._log.debug(
f"Re-connection attempt {i_attemps} after waiting {wait:0.3f} seconds"
)
# reconnect
mapdl._log.debug(
f"Re-connection attempt {i_attemps} after waiting {wait:0.3f} seconds"
)

if not mapdl.is_alive:
connected = mapdl._connect(timeout=wait)
else:
sleep(wait)
if not mapdl.is_alive:
connected = mapdl._connect(timeout=wait)
else:
sleep(wait)

# Retry again
continue
# Retry again
continue

# Custom errors
reason = ""
Expand Down
18 changes: 14 additions & 4 deletions src/ansys/mapdl/core/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

"""Module for miscellaneous functions and methods"""
from enum import Enum
from functools import wraps
from functools import cache, wraps
import importlib
import inspect
import os
Expand Down Expand Up @@ -415,6 +415,16 @@ def write_array(filename: Union[str, bytes], array: np.ndarray) -> None:
np.savetxt(filename, array, fmt="%20.12f")


@cache
def is_package_installed_cached(package_name):
try:
importlib.import_module(package_name)
return True

except ModuleNotFoundError:
return False


def requires_package(package_name: str, softerror: bool = False) -> Callable:
"""
Decorator check whether a package is installed or not.
Expand All @@ -430,11 +440,11 @@ def requires_package(package_name: str, softerror: bool = False) -> Callable:
def decorator(function):
@wraps(function)
def wrapper(self, *args, **kwargs):
try:
importlib.import_module(package_name)

if is_package_installed_cached(package_name):
return function(self, *args, **kwargs)

except ModuleNotFoundError:
else:
msg = (
f"To use the method '{function.__name__}', "
f"the package '{package_name}' is required.\n"
Expand Down
1 change: 1 addition & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import subprocess
import time
from typing import Dict, List
from warnings import warn

import psutil

Expand Down
32 changes: 28 additions & 4 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2749,7 +2749,8 @@ def test_comment_on_debug_mode(mapdl, cleared):

@patch("ansys.mapdl.core.errors.N_ATTEMPTS", 2)
@patch("ansys.mapdl.core.errors.MULTIPLIER_BACKOFF", 1)
def test_timeout_when_exiting(mapdl):
@pytest.mark.parametrize("is_exited", [True, False])
def test_timeout_when_exiting(mapdl, is_exited):
from ansys.mapdl.core import errors

def raise_exception(*args, **kwargs):
Expand All @@ -2759,9 +2760,13 @@ def raise_exception(*args, **kwargs):
e.code = lambda: grpc.StatusCode.ABORTED
e.details = lambda: "My gRPC error details"

# Simulating MAPDL exiting by force
mapdl._exited = is_exited

raise e

handle_generic_grpc_error = errors.handle_generic_grpc_error

with (
patch("ansys.mapdl.core.mapdl_grpc.pb_types.CmdRequest") as mock_cmdrequest,
patch(
Expand All @@ -2787,9 +2792,17 @@ def raise_exception(*args, **kwargs):
assert mapdl._exited

assert mock_handle.call_count == 1
assert mock_connect.call_count == errors.N_ATTEMPTS
assert mock_cmdrequest.call_count == errors.N_ATTEMPTS + 1
assert mock_is_alive.call_count == errors.N_ATTEMPTS + 1

if is_exited:
# Checking no trying to reconnect
assert mock_connect.call_count == 0
assert mock_cmdrequest.call_count == 1
assert mock_is_alive.call_count == 1

else:
assert mock_connect.call_count == errors.N_ATTEMPTS
assert mock_cmdrequest.call_count == errors.N_ATTEMPTS + 1
assert mock_is_alive.call_count == errors.N_ATTEMPTS + 1

mapdl._exited = False

Expand Down Expand Up @@ -2836,3 +2849,14 @@ def test_none_on_selecting(mapdl, cleared, func):

assert len(selfunc("all")) > 0
assert len(selfunc(None)) == 0


def test_requires_package_speed():
from ansys.mapdl.core.misc import requires_package

@requires_package("pyvista")
def my_func(i):
return i + 1

for i in range(1_000_000):
my_func(i)
Loading