forked from sc-zhang/bioscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_cmd_with_parts.py
executable file
·40 lines (32 loc) · 1 KB
/
split_cmd_with_parts.py
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
#!/usr/bin/env python
import sys
import multiprocessing
def write_cmd(fn, cmd_list):
print("\tWriting %s"%fn)
with open(fn, 'w') as fout:
fout.write("".join(cmd_list))
def split_cmd(in_cmd, np, out_str, ts):
print("Loading cmds")
cmd_list = []
with open(in_cmd, 'r') as fin:
for line in fin:
cmd_list.append(line)
print("Splitting commands")
pool = multiprocessing.Pool(processes=ts)
cmd_per_file = int(round(len(cmd_list)/np, 0))
for i in range(0, np):
fn = out_str%(i+1)
if i < np-1:
pool.apply_async(write_cmd, (fn, cmd_list[i*cmd_per_file: (i+1)*cmd_per_file],))
else:
pool.apply_async(write_cmd, (fn, cmd_list[i*cmd_per_file:],))
pool.close()
pool.join()
print("Finished")
if __name__ == "__main__":
if len(sys.argv) < 4:
print("Usage: python "+sys.argv[0]+" <in_cmd_file> <num_parts> <out_str> <threads>")
print("\t<out_str> is a string contain %d as file index, like run_%d.sh")
else:
in_cmd, np, out_str, ts = sys.argv[1:]
split_cmd(in_cmd, int(np), out_str, int(ts))