Repository navigation
Expand file tree
/
Copy pathauto_script.py
More file actions
83 lines (71 loc) · 2.57 KB
/
Copy pathauto_script.py
File metadata and controls
83 lines (71 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import subprocess
from concurrent.futures import ThreadPoolExecutor, as_completed
import subprocess
import time
def run_cmd(cmd, output, timeout=None):
"""
Run command (list form) with subprocess.run, return a dict with results.
cmd: list, e.g. ['python', '-c', 'import time; time.sleep(1)']
timeout: seconds or None
"""
start = time.time()
try:
proc = subprocess.run(cmd, text=True, check=False, timeout=timeout, stdout=open(output, 'wb'))
return {
"cmd": cmd,
"returncode": proc.returncode,
"stdout": proc.stdout,
"stderr": proc.stderr,
"duration": time.time() - start,
"timed_out": False,
}
except subprocess.TimeoutExpired as e:
return {
"cmd": cmd,
"returncode": None,
"stdout": e.stdout or "",
"stderr": str(e),
"duration": time.time() - start,
"timed_out": True,
}
def run_parallel(commands, outputs, max_workers=4, timeout=None):
"""
commands: list of list (each inner is argv list)
outputs: list of output file paths corresponding to each command
max_workers: maximum concurrent subprocesses
timeout: per-process timeout in seconds (or None)
Returns: list of result dicts in order of completion.
"""
results = []
with ThreadPoolExecutor(max_workers=max_workers) as exe:
futures = {exe.submit(run_cmd, cmd, output, timeout): cmd for cmd, output in zip(commands, outputs)}
for fut in as_completed(futures):
res = fut.result()
# simple progress print
print(f"Finished: {res['cmd']} -> rc={res['returncode']} (took {res['duration']:.2f}s)")
results.append(res)
return results
def main():
print('Running set of commands...')
common_commands = [
"python",
"main.py",
"+preset/ifh/qm9=1-4",
"seed=0",
"load_ckp=2",
"enable_log=false",
"compile=false",
"mode=eval",
]
jumps = [1, 2, 5, 10, 20, 25, 50, 100, 250, 500]
options = [f"+options=[remap_cls,djump_{j}]" for j in jumps]
outputs = [f"outputs/djump_1_4_2/djump_{j}.txt" for j in jumps]
# for option, output in zip(options, outputs):
# subprocess.Popen(
# common_commands + [option, "|", "tee", output]
# )
cmds = [common_commands + [option] for option in options]
all_results = run_parallel(cmds, outputs, max_workers=4)
print("All commands completed.")
if __name__ == "__main__":
main()