-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmultitasking.py
More file actions
65 lines (50 loc) · 1.94 KB
/
multitasking.py
File metadata and controls
65 lines (50 loc) · 1.94 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
import multiprocessing as mp
import subprocess
class Task:
def __init__(self, command, name):
self.command = command
self.name = name
class Argument:
def __init__(self, name, values, add_to_name_as=None):
self.name = name
self.values = values
self.add_to_name_as = add_to_name_as
if len(values) > 1:
print_statement = 'please specify a name addition for argument {}, because it has more than one value'.format(
name)
if name != 'run':
assert add_to_name_as is not None, print_statement
def add_argument(tasks, arg):
new_tasks = []
for task in tasks:
for arg_value in arg.values:
if type(arg_value) is list:
arg_name = '-'.join([str(i) for i in arg_value])
new_name = add_to_name(task, arg, arg_name)
arg_command = ' '.join([str(i) for i in arg_value])
new_command = " ".join([task.command, "--{}".format(arg.name), arg_command])
else:
new_name = add_to_name(task, arg, arg_value)
new_command = " ".join([task.command, "--{}".format(arg.name), str(arg_value)])
new_tasks.append(Task(new_command, new_name))
return new_tasks
def add_to_name(task, arg, arg_value):
if arg.add_to_name_as is not None:
new_name = "".join([task.name, arg.add_to_name_as, str(arg_value).zfill(2)])
else:
new_name = task.name
return new_name
def create_tasks_from_arguments(args):
tasks = [Task(command='python main.py', name='')]
for arg in args:
tasks = add_argument(tasks, arg)
for task in tasks:
task.command = " ".join([task.command, '--name', task.name])
return tasks
def run_settings(tasks, n_cpu):
pool = mp.Pool(processes=n_cpu)
pool.map(process_task, tasks)
pool.close()
pool.join()
def process_task(task):
subprocess.call(task.command.split())