-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_2_parallelization.sage
50 lines (40 loc) · 1.61 KB
/
task_2_parallelization.sage
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
import os
import argparse
import psutil
from subprocess import Popen
from time import sleep
from claasp.utils.sage_scripts import get_ciphers, get_cipher_type
def kill_process_and_children(pid, sig=15):
try:
proc = psutil.Process(pid)
except psutil.NoSuchProcess as e:
print('couldn\'t kill process since it no longer exists')
return
for child_process in proc.children(recursive=True):
child_process.send_signal(sig)
proc.send_signal(sig)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='parallelization of task 1')
parser.add_argument('--models', '-m', action="extend", nargs='*',
default=['sat', 'smt', 'milp', 'cp'])
args = parser.parse_args()
if not os.path.exists('scripts/LOGS/task_2/'):
os.makedirs('scripts/LOGS/task_2/')
processes = [
Popen(
['sage', 'scripts/task_2.sage', '-m', model],
stdout=open(f'scripts/LOGS/task_2/{model}.txt', 'a'),
stderr=open(f'scripts/LOGS/task_2/{model}_ERR.txt', 'a')) for model in args.models]
time = 0.0
while len(processes) > 0:
for process in processes:
if (process.poll() is None) and (time < 259200.0):
sleep(1)
time += 1
if process.poll() is not None:
process.communicate()
processes.remove(process)
if (process.poll() is None) and (time >= 259200.0):
print('killing a process, timeout expired')
kill_process_and_children(process.pid)
processes.remove(process)