Skip to content

Commit b88073f

Browse files
authored
Merge pull request #36 from amq92/srun-add_cmd
srun add cmd
2 parents e2e7842 + c9439bd commit b88073f

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

simple_slurm/__about__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.3.4"
1+
__version__ = "0.3.5"

simple_slurm/core.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,30 @@ def script(self, shell: str = "/bin/sh", convert: bool = True):
129129
script = "\n".join((arguments, commands)).strip() + "\n"
130130
return script
131131

132-
def srun(self, run_cmd: str, srun_cmd: str = "srun") -> int:
132+
def srun(self, *run_cmd: str, connector: str = ";", srun_cmd: str = "srun") -> int:
133133
"""Run the srun command with all the (previously) set arguments and
134-
the provided command in 'run_cmd'.
134+
the provided commands in 'run_cmd' alongside with the previously set
135+
commands using 'add_cmd'.
136+
137+
Note that 'run_cmd' can accept multiple arguments. Thus, any of the
138+
other arguments must be given as key-value pairs :
139+
> slurm.srun('echo "Hello"')
140+
> slurm.srun('echo', '"Hello"')
141+
> slurm.srun('echo', '"Hello"', verbose=False)
142+
143+
The 'connector' parameter defines how multiple commands are connected:
144+
- A ; B : Run A and then B, regardless of success of A.
145+
- A && B : Run B if and only if A succeeded.
146+
- A || B : Run B if and only if A failed.
135147
"""
136148
args = (
137149
f"--{self._valid_key(k)}" + f"={v}" if len(v) else ""
138150
for k, v in vars(self.namespace).items()
139151
if v is not None
140152
)
141-
cmd = " ".join((srun_cmd, *args, run_cmd))
142-
153+
self.add_cmd(*run_cmd)
154+
commands = f" {connector} ".join(self.run_cmds)
155+
cmd = " ".join((srun_cmd, *args, commands))
143156
result = subprocess.run(cmd, shell=True, check=True)
144157
return result.returncode
145158

@@ -165,6 +178,7 @@ def sbatch(
165178
> slurm.sbatch('python main.py')
166179
> slurm.sbatch('python', 'main.py')
167180
> slurm.sbatch('python', 'main.py', verbose=False)
181+
168182
This function employs the 'here document' syntax, which requires that
169183
bash variables be scaped. This behavior is default, set 'convert'
170184
to False to disable it.
@@ -176,7 +190,7 @@ def sbatch(
176190
> EOF
177191
178192
For such reason if any bash variable is employed by the 'run_command',
179-
the '$' should be scaped into '\$'. This behavior is default, set
193+
the '$' should be scaped into '\\$'. This behavior is default, set
180194
'convert' to False to disable it.
181195
182196
If the argument 'job_file' is used, the script will be written to the

0 commit comments

Comments
 (0)