-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·65 lines (48 loc) · 1.69 KB
/
run.py
File metadata and controls
executable file
·65 lines (48 loc) · 1.69 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
#!/usr/bin/env python3
import argparse
import glob
import os
import shutil
import subprocess
import sys
import time
parser = argparse.ArgumentParser(description = 'Run planned runs.')
parser.add_argument('--sleep_time', type=float, default = 1.)
parser.add_argument('--max_num_active_processes', type=int, default = 1)
parser.add_argument('--executor', default = 'nohup')
parser.add_argument('--preamble')
parser.add_argument('executable')
args = parser.parse_args()
sleep_time = args.sleep_time
max_num_active_processes = args.max_num_active_processes
executor = args.executor
executable = args.executable
executable = executable.split()
executable[0] = os.path.abspath(executable[0])
executable = ' '.join(executable)
if args.preamble is None:
preamble = ''
else:
with open(args.preamble, 'r') as f:
preamble = f.read()
def count_running():
return len(glob.glob('**/queued', recursive = True)) + len(glob.glob('**/running', recursive = True))
script = """\
#!/usr/bin/env bash
{preamble}
mv queued running
{executable} > out 2> error
rm running
""".format(**globals())
with open('script', 'w') as f:
f.write(script)
os.chmod('script', 0o700)
script_abspath = os.path.abspath('script')
for plan_indicator in sorted(glob.iglob('**/planned', recursive = True)):
d = os.path.dirname(plan_indicator)
print(d)
while count_running() >= max_num_active_processes:
print('{} jobs running, sleeping for {} seconds'.format(count_running(), sleep_time))
time.sleep(sleep_time)
os.rename(plan_indicator, d + '/queued')
subprocess.Popen('cd {d}; {executor} {script_abspath}'.format(**globals()), shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)