|
10 | 10 | from typing import Callable, Optional, Sequence, TYPE_CHECKING |
11 | 11 |
|
12 | 12 | from ..core.workspace import Workspace |
| 13 | +from ..process.runner import run_in_process |
13 | 14 | from .result import SpeculationResult, SpeculationOutcome |
14 | 15 |
|
15 | 16 | if TYPE_CHECKING: |
@@ -90,22 +91,17 @@ def _run_candidate(index: int) -> None: |
90 | 91 | ) as b: |
91 | 92 | result.branch_path = b.path |
92 | 93 | try: |
93 | | - if self._resource_limits is not None: |
94 | | - from ..process.runner import run_in_process |
95 | | - |
96 | | - def _on_scope(sp: Path, _i: int = index) -> None: |
97 | | - branch_scopes[_i] = sp |
98 | | - |
99 | | - ret = run_in_process( |
100 | | - self._task, (b.path, index), |
101 | | - workspace=b.path, |
102 | | - limits=self._resource_limits, |
103 | | - parent_cgroup=root_cgroup, |
104 | | - scope_callback=_on_scope, |
105 | | - ) |
106 | | - success, score = ret |
107 | | - else: |
108 | | - success, score = self._task(b.path, index) |
| 94 | + def _on_scope(sp: Path, _i: int = index) -> None: |
| 95 | + branch_scopes[_i] = sp |
| 96 | + |
| 97 | + ret = run_in_process( |
| 98 | + self._task, (b.path, index), |
| 99 | + workspace=b.path, |
| 100 | + limits=self._resource_limits, |
| 101 | + parent_cgroup=root_cgroup, |
| 102 | + scope_callback=_on_scope if self._resource_limits else None, |
| 103 | + ) |
| 104 | + success, score = ret |
109 | 105 | result.success = bool(success) |
110 | 106 | result.score = score |
111 | 107 | result.return_value = (success, score) |
@@ -254,16 +250,12 @@ def _run(self, workspace: Workspace, root_cgroup: Optional[Path]) -> Speculation |
254 | 250 | branch_name, on_success=None, on_error="abort" |
255 | 251 | ) as b: |
256 | 252 | result.branch_path = b.path |
257 | | - if self._resource_limits is not None: |
258 | | - from ..process.runner import run_in_process |
259 | | - success = run_in_process( |
260 | | - self._task, (b.path, attempt, feedback), |
261 | | - workspace=b.path, |
262 | | - limits=self._resource_limits, |
263 | | - parent_cgroup=root_cgroup, |
264 | | - ) |
265 | | - else: |
266 | | - success = self._task(b.path, attempt, feedback) |
| 253 | + success = run_in_process( |
| 254 | + self._task, (b.path, attempt, feedback), |
| 255 | + workspace=b.path, |
| 256 | + limits=self._resource_limits, |
| 257 | + parent_cgroup=root_cgroup, |
| 258 | + ) |
267 | 259 | result.success = bool(success) |
268 | 260 | result.return_value = success |
269 | 261 |
|
@@ -420,21 +412,16 @@ def _run(index: int) -> None: |
420 | 412 | ) as b: |
421 | 413 | result.branch_path = b.path |
422 | 414 | try: |
423 | | - if self._resource_limits is not None: |
424 | | - from ..process.runner import run_in_process |
425 | | - |
426 | | - def _on_scope(sp: Path, _i: int = index) -> None: |
427 | | - branch_scopes[_i] = sp |
428 | | - |
429 | | - ret = run_in_process( |
430 | | - strategies[index], (b.path,), |
431 | | - workspace=b.path, |
432 | | - limits=self._resource_limits, |
433 | | - parent_cgroup=parent_cgroup, |
434 | | - scope_callback=_on_scope, |
435 | | - ) |
436 | | - else: |
437 | | - ret = strategies[index](b.path) |
| 415 | + def _on_scope(sp: Path, _i: int = index) -> None: |
| 416 | + branch_scopes[_i] = sp |
| 417 | + |
| 418 | + ret = run_in_process( |
| 419 | + strategies[index], (b.path,), |
| 420 | + workspace=b.path, |
| 421 | + limits=self._resource_limits, |
| 422 | + parent_cgroup=parent_cgroup, |
| 423 | + scope_callback=_on_scope if self._resource_limits else None, |
| 424 | + ) |
438 | 425 | if isinstance(ret, tuple): |
439 | 426 | success, score = ret |
440 | 427 | else: |
@@ -702,21 +689,16 @@ def _beam_worker(index: int) -> None: |
702 | 689 | result.branch_path = b.path |
703 | 690 | beam_branches[index] = b |
704 | 691 | try: |
705 | | - if self._resource_limits is not None: |
706 | | - from ..process.runner import run_in_process |
707 | | - |
708 | | - def _on_scope(sp: Path, _i: int = index) -> None: |
709 | | - beam_task_scopes[_i] = sp |
710 | | - |
711 | | - ret = run_in_process( |
712 | | - self._strategies[index], (b.path,), |
713 | | - workspace=b.path, |
714 | | - limits=self._resource_limits, |
715 | | - parent_cgroup=beam_cgroups[index], |
716 | | - scope_callback=_on_scope, |
717 | | - ) |
718 | | - else: |
719 | | - ret = self._strategies[index](b.path) |
| 692 | + def _on_scope(sp: Path, _i: int = index) -> None: |
| 693 | + beam_task_scopes[_i] = sp |
| 694 | + |
| 695 | + ret = run_in_process( |
| 696 | + self._strategies[index], (b.path,), |
| 697 | + workspace=b.path, |
| 698 | + limits=self._resource_limits, |
| 699 | + parent_cgroup=beam_cgroups[index], |
| 700 | + scope_callback=_on_scope if self._resource_limits else None, |
| 701 | + ) |
720 | 702 | result.success, result.score = self._score( |
721 | 703 | ret, b.path |
722 | 704 | ) |
@@ -813,23 +795,18 @@ def _sub_worker(idx: int, _d: int = _depth) -> None: |
813 | 795 | ) as sb: |
814 | 796 | result.branch_path = sb.path |
815 | 797 | try: |
816 | | - if self._resource_limits is not None: |
817 | | - from ..process.runner import run_in_process |
818 | | - |
819 | | - def _on_sub_scope( |
820 | | - sp: Path, _j: int = idx, |
821 | | - ) -> None: |
822 | | - sub_scopes[_j] = sp |
823 | | - |
824 | | - ret = run_in_process( |
825 | | - strategy, (sb.path,), |
826 | | - workspace=sb.path, |
827 | | - limits=self._resource_limits, |
828 | | - parent_cgroup=beam_cgroups[beam_idx], |
829 | | - scope_callback=_on_sub_scope, |
830 | | - ) |
831 | | - else: |
832 | | - ret = strategy(sb.path) |
| 798 | + def _on_sub_scope( |
| 799 | + sp: Path, _j: int = idx, |
| 800 | + ) -> None: |
| 801 | + sub_scopes[_j] = sp |
| 802 | + |
| 803 | + ret = run_in_process( |
| 804 | + strategy, (sb.path,), |
| 805 | + workspace=sb.path, |
| 806 | + limits=self._resource_limits, |
| 807 | + parent_cgroup=beam_cgroups[beam_idx], |
| 808 | + scope_callback=_on_sub_scope if self._resource_limits else None, |
| 809 | + ) |
833 | 810 | result.success, result.score = self._score( |
834 | 811 | ret, sb.path |
835 | 812 | ) |
@@ -1038,21 +1015,16 @@ def _run_candidate(index: int) -> None: |
1038 | 1015 | result.branch_path = b.path |
1039 | 1016 | branch_paths[index] = b.path |
1040 | 1017 | try: |
1041 | | - if self._resource_limits is not None: |
1042 | | - from ..process.runner import run_in_process |
1043 | | - |
1044 | | - def _on_scope(sp: Path, _i: int = index) -> None: |
1045 | | - branch_scopes[_i] = sp |
1046 | | - |
1047 | | - success = run_in_process( |
1048 | | - self._task, (b.path, index), |
1049 | | - workspace=b.path, |
1050 | | - limits=self._resource_limits, |
1051 | | - parent_cgroup=root_cgroup, |
1052 | | - scope_callback=_on_scope, |
1053 | | - ) |
1054 | | - else: |
1055 | | - success = self._task(b.path, index) |
| 1018 | + def _on_scope(sp: Path, _i: int = index) -> None: |
| 1019 | + branch_scopes[_i] = sp |
| 1020 | + |
| 1021 | + success = run_in_process( |
| 1022 | + self._task, (b.path, index), |
| 1023 | + workspace=b.path, |
| 1024 | + limits=self._resource_limits, |
| 1025 | + parent_cgroup=root_cgroup, |
| 1026 | + scope_callback=_on_scope if self._resource_limits else None, |
| 1027 | + ) |
1056 | 1028 | result.success = bool(success) |
1057 | 1029 | result.return_value = success |
1058 | 1030 | except Exception as e: |
|
0 commit comments