Skip to content

Commit d238967

Browse files
committed
[Maintenance] Refactor pysqa interface
1 parent 7346897 commit d238967

10 files changed

Lines changed: 43 additions & 43 deletions

File tree

src/executorlib/executor/single.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ def __init__(
393393
)
394394
if not plot_dependency_graph:
395395
from executorlib.task_scheduler.file.spawner_subprocess import (
396-
execute_in_subprocess,
396+
subprocess_execute,
397397
)
398398
from executorlib.task_scheduler.file.task_scheduler import (
399399
create_file_executor,
@@ -415,7 +415,7 @@ def __init__(
415415
block_allocation=block_allocation,
416416
init_function=init_function,
417417
disable_dependencies=disable_dependencies,
418-
execute_function=execute_in_subprocess,
418+
execute_function=subprocess_execute,
419419
wait=wait,
420420
refresh_rate=refresh_rate,
421421
validator=validate_resource_dict_with_optional_keys,

src/executorlib/standalone/scheduler.py renamed to src/executorlib/standalone/command_pysqa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pysqa import QueueAdapter
66

77

8-
def terminate_with_pysqa(
8+
def pysqa_terminate(
99
queue_id: int,
1010
config_directory: Optional[str] = None,
1111
backend: Optional[str] = None,

src/executorlib/task_scheduler/file/shared.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from executorlib.standalone.command import get_cache_execute_command
99
from executorlib.standalone.hdf import get_cache_files, get_output, get_queue_id
1010
from executorlib.standalone.serialize import serialize_funct
11-
from executorlib.task_scheduler.file.spawner_subprocess import terminate_subprocess
11+
from executorlib.task_scheduler.file.spawner_subprocess import subprocess_terminate
1212

1313

1414
class FutureItem:
@@ -381,7 +381,7 @@ def _cancel_processes(
381381
pysqa_config_directory (str): path to the pysqa config directory (only for pysqa based backend).
382382
backend (str): name of the backend used to spawn tasks.
383383
"""
384-
if terminate_function is not None and terminate_function == terminate_subprocess:
384+
if terminate_function is not None and terminate_function == subprocess_terminate:
385385
for task in process_dict.values():
386386
terminate_function(task=task)
387387
elif terminate_function is not None and backend is not None:

src/executorlib/task_scheduler/file/spawner_pysqa.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from executorlib.standalone.interactive.spawner import (
1010
set_current_directory_in_environment,
1111
)
12-
from executorlib.standalone.scheduler import pysqa_execute_command, terminate_with_pysqa
12+
from executorlib.standalone.command_pysqa import pysqa_execute_command, pysqa_terminate
1313

1414

1515
def execute_with_pysqa(
@@ -122,7 +122,7 @@ def terminate_tasks_in_cache(
122122
for f in hdf5_file_lst:
123123
queue_id = get_queue_id(f)
124124
if queue_id is not None:
125-
terminate_with_pysqa(
125+
pysqa_terminate(
126126
queue_id=queue_id,
127127
config_directory=pysqa_config_directory,
128128
backend=backend,
@@ -148,7 +148,7 @@ def terminate_task_in_cache(
148148
file_name = os.path.join(cache_directory, cache_key + "_i.h5")
149149
queue_id = get_queue_id(file_name=file_name)
150150
if queue_id is not None:
151-
terminate_with_pysqa(
151+
pysqa_terminate(
152152
queue_id=queue_id,
153153
config_directory=pysqa_config_directory,
154154
backend=backend,

src/executorlib/task_scheduler/file/spawner_subprocess.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
)
1111

1212

13-
def execute_in_subprocess(
13+
def subprocess_execute(
1414
command: list,
1515
file_name: str,
1616
data_dict: dict,
@@ -65,7 +65,7 @@ def execute_in_subprocess(
6565
return subprocess.Popen(command, universal_newlines=True, cwd=cwd)
6666

6767

68-
def terminate_subprocess(task):
68+
def subprocess_terminate(task):
6969
"""
7070
Terminate a subprocess and wait for it to complete.
7171

src/executorlib/task_scheduler/file/task_scheduler.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
from executorlib.task_scheduler.base import TaskSchedulerBase, validate_resource_dict
1313
from executorlib.task_scheduler.file.shared import execute_tasks_h5
1414
from executorlib.task_scheduler.file.spawner_subprocess import (
15-
execute_in_subprocess,
16-
terminate_subprocess,
15+
subprocess_execute,
16+
subprocess_terminate,
1717
)
1818

1919
try:
20-
from executorlib.standalone.scheduler import terminate_with_pysqa
20+
from executorlib.standalone.command_pysqa import pysqa_terminate
2121
from executorlib.task_scheduler.file.spawner_pysqa import execute_with_pysqa
2222
except ImportError:
2323
# If pysqa is not available fall back to executing tasks in a subprocess
24-
execute_with_pysqa = execute_in_subprocess # type: ignore
25-
terminate_with_pysqa = None # type: ignore
24+
execute_with_pysqa = subprocess_execute # type: ignore
25+
pysqa_terminate = None # type: ignore
2626

2727

2828
class FileTaskScheduler(TaskSchedulerBase):
@@ -126,10 +126,10 @@ def create_file_executor(
126126
check_executor(executor=flux_executor)
127127
check_nested_flux_executor(nested_flux_executor=flux_executor_nesting)
128128
check_flux_log_files(flux_log_files=flux_log_files)
129-
if execute_function != execute_in_subprocess:
130-
terminate_function = terminate_with_pysqa # type: ignore
129+
if execute_function != subprocess_execute:
130+
terminate_function = pysqa_terminate # type: ignore
131131
else:
132-
terminate_function = terminate_subprocess # type: ignore
132+
terminate_function = subprocess_terminate # type: ignore
133133
return FileTaskScheduler(
134134
executor_kwargs=executor_kwargs,
135135
pysqa_config_directory=pysqa_config_directory,

src/executorlib/task_scheduler/interactive/spawner_pysqa.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
BaseSpawner,
1111
set_current_directory_in_environment,
1212
)
13-
from executorlib.standalone.scheduler import pysqa_execute_command, terminate_with_pysqa
13+
from executorlib.standalone.command_pysqa import pysqa_execute_command, pysqa_terminate
1414
from executorlib.task_scheduler.base import validate_resource_dict
1515
from executorlib.task_scheduler.interactive.blockallocation import (
1616
BlockAllocationTaskScheduler,
@@ -162,7 +162,7 @@ def shutdown(self, wait: bool = True):
162162
wait (bool, optional): Whether to wait for the interface to shutdown. Defaults to True.
163163
"""
164164
if self._process is not None:
165-
terminate_with_pysqa(
165+
pysqa_terminate(
166166
queue_id=self._process,
167167
config_directory=self._config_directory,
168168
backend=self._backend,

tests/unit/executor/test_flux_cluster.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from executorlib import terminate_tasks_in_cache, terminate_task_in_cache
1414
from executorlib.standalone.hdf import dump
1515
from executorlib.task_scheduler.file.spawner_pysqa import execute_with_pysqa
16-
from executorlib.standalone.scheduler import terminate_with_pysqa
16+
from executorlib.standalone.command_pysqa import pysqa_terminate
1717
from executorlib.task_scheduler.interactive.spawner_pysqa import PysqaSpawner
1818

1919
skip_flux_test = "FLUX_URI" not in os.environ
@@ -245,7 +245,7 @@ def test_pysqa_interface(self):
245245
cache_directory="executorlib_cache",
246246
backend="flux"
247247
)
248-
self.assertIsNone(terminate_with_pysqa(queue_id=queue_id, backend="flux"))
248+
self.assertIsNone(pysqa_terminate(queue_id=queue_id, backend="flux"))
249249

250250
def test_executor_existing_files(self):
251251
with FluxClusterExecutor(

tests/unit/task_scheduler/file/test_mpi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
try:
77
from executorlib.task_scheduler.file.task_scheduler import FileTaskScheduler
8-
from executorlib.task_scheduler.file.spawner_subprocess import execute_in_subprocess
8+
from executorlib.task_scheduler.file.spawner_subprocess import subprocess_execute
99

1010
skip_h5py_test = False
1111
except ImportError:
@@ -30,7 +30,7 @@ def mpi_funct(i):
3030
class TestCacheExecutorMPI(unittest.TestCase):
3131
def test_executor(self):
3232
with FileTaskScheduler(
33-
executor_kwargs={"cores": 2}, execute_function=execute_in_subprocess
33+
executor_kwargs={"cores": 2}, execute_function=subprocess_execute
3434
) as exe:
3535
fs1 = exe.submit(mpi_funct, 1)
3636
self.assertFalse(fs1.done())

tests/unit/task_scheduler/file/test_serial.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
try:
1010
from executorlib.task_scheduler.file.spawner_subprocess import (
11-
execute_in_subprocess,
12-
terminate_subprocess,
11+
subprocess_execute,
12+
subprocess_terminate,
1313
)
1414
from executorlib.task_scheduler.file.task_scheduler import FileTaskScheduler, create_file_executor
1515
from executorlib.task_scheduler.file.shared import execute_tasks_h5, _convert_args_and_kwargs
@@ -36,21 +36,21 @@ def get_error(a):
3636
)
3737
class TestCacheExecutorSerial(unittest.TestCase):
3838
def test_submit_with_positional_and_keyword_args(self):
39-
with FileTaskScheduler(execute_function=execute_in_subprocess) as exe:
39+
with FileTaskScheduler(execute_function=subprocess_execute) as exe:
4040
fs1 = exe.submit(my_funct, 1, b=2)
4141
self.assertFalse(fs1.done())
4242
self.assertEqual(fs1.result(), 3)
4343
self.assertTrue(fs1.done())
4444

4545
def test_submit_with_custom_cache_key(self):
46-
with FileTaskScheduler(execute_function=execute_in_subprocess) as exe:
46+
with FileTaskScheduler(execute_function=subprocess_execute) as exe:
4747
fs1 = exe.submit(my_funct, 1, b=2, resource_dict={"cache_key": "a/b/c"})
4848
self.assertFalse(fs1.done())
4949
self.assertEqual(fs1.result(), 3)
5050
self.assertTrue(fs1.done())
5151

5252
def test_submit_dependency_with_keyword_arg_future(self):
53-
with FileTaskScheduler(execute_function=execute_in_subprocess) as exe:
53+
with FileTaskScheduler(execute_function=subprocess_execute) as exe:
5454
fs1 = exe.submit(my_funct, 1, b=2)
5555
fs2 = exe.submit(my_funct, 1, b=fs1)
5656
self.assertFalse(fs2.done())
@@ -70,23 +70,23 @@ def test_create_file_executor_error(self):
7070
def test_submit_dependency_raises_when_dependencies_disabled(self):
7171
with self.assertRaises(ValueError):
7272
with FileTaskScheduler(
73-
execute_function=execute_in_subprocess, disable_dependencies=True
73+
execute_function=subprocess_execute, disable_dependencies=True
7474
) as exe:
7575
fs = exe.submit(my_funct, 1, b=exe.submit(my_funct, 1, b=2))
7676
fs.result()
7777

7878
def test_executor_working_directory(self):
7979
cwd = os.path.join(os.path.dirname(__file__), "..", "..", "..", "static")
8080
with FileTaskScheduler(
81-
executor_kwargs={"cwd": cwd}, execute_function=execute_in_subprocess
81+
executor_kwargs={"cwd": cwd}, execute_function=subprocess_execute
8282
) as exe:
8383
fs1 = exe.submit(list_files_in_working_directory)
8484
self.assertEqual(fs1.result(), os.listdir(cwd))
8585

8686
def test_executor_error(self):
8787
cwd = os.path.join(os.path.dirname(__file__), "..", "..", "..", "static")
8888
with FileTaskScheduler(
89-
executor_kwargs={"cwd": cwd}, execute_function=execute_in_subprocess
89+
executor_kwargs={"cwd": cwd}, execute_function=subprocess_execute
9090
) as exe:
9191
fs1 = exe.submit(get_error, a=1)
9292
with self.assertRaises(ValueError):
@@ -97,7 +97,7 @@ def test_executor_error_file(self):
9797
cwd = os.path.join(os.path.dirname(__file__), "..", "..", "..", "static")
9898
with FileTaskScheduler(
9999
executor_kwargs={"cwd": cwd, "error_log_file": "error.out"},
100-
execute_function=execute_in_subprocess
100+
execute_function=subprocess_execute
101101
) as exe:
102102
fs1 = exe.submit(get_error, a=1)
103103
with self.assertRaises(ValueError):
@@ -125,9 +125,9 @@ def test_executor_function(self):
125125
target=execute_tasks_h5,
126126
kwargs={
127127
"future_queue": q,
128-
"execute_function": execute_in_subprocess,
128+
"execute_function": subprocess_execute,
129129
"executor_kwargs": {"cores": 1, "cwd": None, "cache_directory": cache_dir},
130-
"terminate_function": terminate_subprocess,
130+
"terminate_function": subprocess_terminate,
131131
},
132132
)
133133
process.start()
@@ -165,9 +165,9 @@ def test_executor_function_dependence_kwargs(self):
165165
target=execute_tasks_h5,
166166
kwargs={
167167
"future_queue": q,
168-
"execute_function": execute_in_subprocess,
168+
"execute_function": subprocess_execute,
169169
"executor_kwargs": {"cores": 1, "cwd": None, "cache_directory": cache_dir},
170-
"terminate_function": terminate_subprocess,
170+
"terminate_function": subprocess_terminate,
171171
},
172172
)
173173
process.start()
@@ -205,9 +205,9 @@ def test_executor_function_dependence_args(self):
205205
target=execute_tasks_h5,
206206
kwargs={
207207
"future_queue": q,
208-
"execute_function": execute_in_subprocess,
208+
"execute_function": subprocess_execute,
209209
"executor_kwargs": {"cores": 1, "cache_directory": cache_dir},
210-
"terminate_function": terminate_subprocess,
210+
"terminate_function": subprocess_terminate,
211211
},
212212
)
213213
process.start()
@@ -218,27 +218,27 @@ def test_executor_function_dependence_args(self):
218218
process.join()
219219

220220
def test_execute_in_subprocess(self):
221-
process = execute_in_subprocess(
221+
process = subprocess_execute(
222222
command=["sleep", "5"],
223223
file_name="test.h5",
224224
data_dict={"fn": sleep, "args": (5,)},
225225
)
226-
self.assertIsNone(terminate_subprocess(task=process))
226+
self.assertIsNone(subprocess_terminate(task=process))
227227

228228
def test_execute_in_subprocess_errors(self):
229229
file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), "executorlib_cache", "test.h5"))
230230
os.makedirs(os.path.dirname(file_name), exist_ok=True)
231231
with open(file_name, "w") as f:
232232
f.write("test")
233233
with self.assertRaises(ValueError):
234-
execute_in_subprocess(
234+
subprocess_execute(
235235
file_name=file_name,
236236
data_dict={},
237237
command=[],
238238
config_directory="test",
239239
)
240240
with self.assertRaises(ValueError):
241-
execute_in_subprocess(
241+
subprocess_execute(
242242
file_name=file_name,
243243
data_dict={},
244244
command=[],

0 commit comments

Comments
 (0)