Skip to content

Commit 9d8fb78

Browse files
Set max_cores and max_workers to None (#478)
* Set max_cores and max_workers to None * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * By default get the number of cores from the multiprocessing package * Test Fixes * more fixes * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d85a615 commit 9d8fb78

File tree

7 files changed

+43
-20
lines changed

7 files changed

+43
-20
lines changed

executorlib/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ class Executor:
8383

8484
def __init__(
8585
self,
86-
max_workers: int = 1,
86+
max_workers: Optional[int] = None,
8787
backend: str = "local",
8888
cache_directory: Optional[str] = None,
89-
max_cores: int = 1,
89+
max_cores: Optional[int] = None,
9090
resource_dict: Optional[dict] = None,
9191
flux_executor=None,
9292
flux_executor_pmi_mode: Optional[str] = None,
@@ -104,10 +104,10 @@ def __init__(
104104

105105
def __new__(
106106
cls,
107-
max_workers: int = 1,
107+
max_workers: Optional[int] = None,
108108
backend: str = "local",
109109
cache_directory: Optional[str] = None,
110-
max_cores: int = 1,
110+
max_cores: Optional[int] = None,
111111
resource_dict: Optional[dict] = None,
112112
flux_executor=None,
113113
flux_executor_pmi_mode: Optional[str] = None,

executorlib/interactive/executor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ def __exit__(
147147

148148

149149
def create_executor(
150-
max_workers: int = 1,
150+
max_workers: Optional[int] = None,
151151
backend: str = "local",
152-
max_cores: int = 1,
152+
max_cores: Optional[int] = None,
153153
cache_directory: Optional[str] = None,
154154
resource_dict: Optional[dict] = None,
155155
flux_executor=None,

executorlib/standalone/inputcheck.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect
2+
import multiprocessing
23
from concurrent.futures import Executor
34
from typing import Callable, List, Optional
45

@@ -131,12 +132,14 @@ def check_init_function(block_allocation: bool, init_function: Callable) -> None
131132
raise ValueError("")
132133

133134

134-
def check_max_workers_and_cores(max_workers: int, max_cores: int) -> None:
135-
if max_workers != 1:
135+
def check_max_workers_and_cores(
136+
max_workers: Optional[int], max_cores: Optional[int]
137+
) -> None:
138+
if max_workers is not None:
136139
raise ValueError(
137140
"The number of workers cannot be controlled with the pysqa based backend."
138141
)
139-
if max_cores != 1:
142+
if max_cores is not None:
140143
raise ValueError(
141144
"The number of cores cannot be controlled with the pysqa based backend."
142145
)
@@ -166,10 +169,15 @@ def check_pysqa_config_directory(pysqa_config_directory: Optional[str]) -> None:
166169
)
167170

168171

169-
def validate_number_of_cores(max_cores: int, max_workers: int) -> int:
172+
def validate_number_of_cores(
173+
max_cores: Optional[int], max_workers: Optional[int]
174+
) -> int:
170175
"""
171176
Validate the number of cores and return the appropriate value.
172177
"""
173-
if max_workers != 1 and max_cores == 1:
178+
if max_workers is None and max_cores is None:
179+
return multiprocessing.cpu_count()
180+
elif max_workers is not None and max_cores is None:
174181
return max_workers
175-
return max_cores
182+
else:
183+
return max_cores

tests/test_integration_pyiron_workflow.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def slowly_returns_dynamic(dynamic_arg):
7474
return dynamic_arg
7575

7676
dynamic_dynamic = slowly_returns_dynamic()
77-
executor = Executor(block_allocation=True)
77+
executor = Executor(block_allocation=True, max_workers=1)
7878
cloudpickle_register(ind=1)
7979
dynamic_object = does_nothing()
8080
fs = executor.submit(dynamic_dynamic.run, dynamic_object)
@@ -104,7 +104,7 @@ def slowly_returns_42():
104104
self.assertIsNone(
105105
dynamic_42.result, msg="Just a sanity check that the test is set up right"
106106
)
107-
executor = Executor(block_allocation=True)
107+
executor = Executor(block_allocation=True, max_workers=1)
108108
cloudpickle_register(ind=1)
109109
fs = executor.submit(dynamic_42.run)
110110
fs.add_done_callback(dynamic_42.process_result)
@@ -135,7 +135,7 @@ def returns_42():
135135
dynamic_42.running,
136136
msg="Sanity check that the test starts in the expected condition",
137137
)
138-
executor = Executor(block_allocation=True)
138+
executor = Executor(block_allocation=True, max_workers=1)
139139
cloudpickle_register(ind=1)
140140
fs = executor.submit(dynamic_42.run)
141141
fs.add_done_callback(dynamic_42.process_result)
@@ -159,7 +159,7 @@ def raise_error():
159159
raise RuntimeError
160160

161161
re = raise_error()
162-
executor = Executor(block_allocation=True)
162+
executor = Executor(block_allocation=True, max_workers=1)
163163
cloudpickle_register(ind=1)
164164
fs = executor.submit(re.run)
165165
with self.assertRaises(
@@ -189,7 +189,7 @@ def slowly_returns_dynamic():
189189
return inside_variable
190190

191191
dynamic_dynamic = slowly_returns_dynamic()
192-
executor = Executor(block_allocation=True)
192+
executor = Executor(block_allocation=True, max_workers=1)
193193
cloudpickle_register(ind=1)
194194
fs = executor.submit(dynamic_dynamic.run)
195195
self.assertIsInstance(
@@ -218,7 +218,7 @@ def slow():
218218
return fortytwo
219219

220220
f = slow()
221-
executor = Executor(block_allocation=True)
221+
executor = Executor(block_allocation=True, max_workers=1)
222222
cloudpickle_register(ind=1)
223223
fs = executor.submit(f.run)
224224
self.assertEqual(

tests/test_local_executor_future.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def submit():
6868
# Executor only exists in this scope and can get garbage collected after
6969
# this function is exits
7070
future = InteractiveExecutor(
71+
max_workers=1,
7172
executor_kwargs={},
7273
spawner=MpiExecSpawner,
7374
).submit(slow_callable)
@@ -108,6 +109,7 @@ def run(self):
108109
self.running = True
109110

110111
future = InteractiveExecutor(
112+
max_workers=1,
111113
executor_kwargs={},
112114
spawner=MpiExecSpawner,
113115
).submit(self.return_42)

tests/test_shared_input_check.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
check_max_workers_and_cores,
1818
check_hostname_localhost,
1919
check_pysqa_config_directory,
20+
validate_number_of_cores,
2021
)
2122

2223

@@ -80,9 +81,9 @@ def test_check_flux_executor_pmi_mode(self):
8081

8182
def test_check_max_workers_and_cores(self):
8283
with self.assertRaises(ValueError):
83-
check_max_workers_and_cores(max_workers=2, max_cores=1)
84+
check_max_workers_and_cores(max_workers=2, max_cores=None)
8485
with self.assertRaises(ValueError):
85-
check_max_workers_and_cores(max_workers=1, max_cores=2)
86+
check_max_workers_and_cores(max_workers=None, max_cores=2)
8687
with self.assertRaises(ValueError):
8788
check_max_workers_and_cores(max_workers=2, max_cores=2)
8889

@@ -95,3 +96,14 @@ def test_check_hostname_localhost(self):
9596
def test_check_pysqa_config_directory(self):
9697
with self.assertRaises(ValueError):
9798
check_pysqa_config_directory(pysqa_config_directory="path/to/config")
99+
100+
def test_validate_number_of_cores(self):
101+
self.assertIsInstance(
102+
validate_number_of_cores(max_cores=None, max_workers=None), int
103+
)
104+
self.assertIsInstance(
105+
validate_number_of_cores(max_cores=1, max_workers=None), int
106+
)
107+
self.assertIsInstance(
108+
validate_number_of_cores(max_cores=None, max_workers=1), int
109+
)

tests/test_shell_interactive.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def test_execute_single_task(self):
105105
def test_shell_interactive_executor(self):
106106
cloudpickle_register(ind=1)
107107
with Executor(
108+
max_workers=1,
108109
init_function=init_process,
109110
block_allocation=True,
110111
) as exe:

0 commit comments

Comments
 (0)