@@ -129,17 +129,30 @@ def script(self, shell: str = "/bin/sh", convert: bool = True):
129
129
script = "\n " .join ((arguments , commands )).strip () + "\n "
130
130
return script
131
131
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 :
133
133
"""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.
135
147
"""
136
148
args = (
137
149
f"--{ self ._valid_key (k )} " + f"={ v } " if len (v ) else ""
138
150
for k , v in vars (self .namespace ).items ()
139
151
if v is not None
140
152
)
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 ))
143
156
result = subprocess .run (cmd , shell = True , check = True )
144
157
return result .returncode
145
158
@@ -165,6 +178,7 @@ def sbatch(
165
178
> slurm.sbatch('python main.py')
166
179
> slurm.sbatch('python', 'main.py')
167
180
> slurm.sbatch('python', 'main.py', verbose=False)
181
+
168
182
This function employs the 'here document' syntax, which requires that
169
183
bash variables be scaped. This behavior is default, set 'convert'
170
184
to False to disable it.
@@ -176,7 +190,7 @@ def sbatch(
176
190
> EOF
177
191
178
192
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
180
194
'convert' to False to disable it.
181
195
182
196
If the argument 'job_file' is used, the script will be written to the
0 commit comments