Skip to content

Commit a8b1377

Browse files
committed
Make the recursion limit workaround dependent on the aiida-core version
With aiida-core v2.8.0 we switched from nest-asyncio to greenlet and therefore do not need to dynamically increase the recursion limit anymore.
1 parent 4df7350 commit a8b1377

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

src/aiida_pythonjob/decorator.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@
77
import typing as t
88
from typing import List
99

10-
from aiida.engine.processes.functions import FunctionType, get_stack_size
10+
from packaging.version import parse as parse_version
11+
12+
import aiida
13+
from aiida.engine.processes.functions import FunctionType
1114
from aiida.manage import get_manager
1215
from aiida.orm import ProcessNode
16+
17+
_AIIDA_VERSION = parse_version(aiida.__version__)
18+
_NEEDS_RECURSION_LIMIT_WORKAROUND = _AIIDA_VERSION < parse_version("2.8.0")
19+
20+
if _NEEDS_RECURSION_LIMIT_WORKAROUND:
21+
from aiida.engine.processes.functions import get_stack_size
1322
from node_graph.socket_spec import SocketSpec
1423

1524
from aiida_pythonjob.calculations.pyfunction import PyFunction
@@ -42,25 +51,26 @@ def run_get_node(*args, **kwargs) -> tuple[dict[str, t.Any] | None, "ProcessNode
4251
:param kwargs: input keyword arguments to construct the FunctionProcess
4352
:return: tuple of the outputs of the process and the process node
4453
"""
45-
frame_delta = 1000
46-
frame_count = get_stack_size()
47-
stack_limit = sys.getrecursionlimit()
48-
LOGGER.info("Executing process function, current stack status: %d frames of %d", frame_count, stack_limit)
49-
50-
# If the current frame count is more than 80% of the stack limit, or comes within 200 frames, increase the
51-
# stack limit by ``frame_delta``.
52-
if frame_count > min(0.8 * stack_limit, stack_limit - 200):
53-
LOGGER.warning(
54-
"Current stack contains %d frames which is close to the limit of %d. Increasing the limit by %d",
55-
frame_count,
56-
stack_limit,
57-
frame_delta,
54+
if _NEEDS_RECURSION_LIMIT_WORKAROUND:
55+
frame_delta = 1000
56+
frame_count = get_stack_size()
57+
stack_limit = sys.getrecursionlimit()
58+
LOGGER.info(
59+
"Executing process function, current stack status: %d frames of %d", frame_count, stack_limit
5860
)
59-
sys.setrecursionlimit(stack_limit + frame_delta)
61+
62+
if frame_count > min(0.8 * stack_limit, stack_limit - 200):
63+
LOGGER.warning(
64+
"Current stack contains %d frames which is close to the limit of %d. Increasing the limit by %d",
65+
frame_count,
66+
stack_limit,
67+
frame_delta,
68+
)
69+
sys.setrecursionlimit(stack_limit + frame_delta)
6070

6171
manager = get_manager()
6272
runner = manager.get_runner()
63-
# # Remove all the known inputs from the kwargs
73+
# Remove all the known inputs from the kwargs
6474
outputs_spec = kwargs.pop("outputs_spec", None) or outputs
6575
inputs_spec = kwargs.pop("inputs_spec", None) or inputs
6676
metadata = kwargs.pop("metadata", None)

0 commit comments

Comments
 (0)