-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun_with_timeout.py
55 lines (43 loc) · 1.11 KB
/
run_with_timeout.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import time
from multiprocessing import Pool
from typing import Optional
from func_timeout import func_timeout, FunctionTimedOut
from mapfmclient import Problem
from tqdm import tqdm
from python.algorithm import MapfAlgorithm
def run_problem_with_timeout_star(args):
return run_problem_with_timeout(*args)
def run_problem_with_timeout(
algorithm: MapfAlgorithm,
problem: Problem,
timeout: int = 2 * 60,
) -> Optional[float]:
start = time.time()
try:
func_timeout(
timeout,
algorithm.solve,
(problem,),
)
except FunctionTimedOut:
return None
except Exception as e:
print(e)
return None
end = time.time()
return end - start
def run_with_timeout(
p: Pool,
algorithm: MapfAlgorithm,
problems: list[Problem],
timeout: int = 2 * 60,
) -> list[Optional[float]]:
return list(
tqdm(
p.imap(
run_problem_with_timeout_star,
[(algorithm, problem, timeout) for problem in problems],
),
total=len(problems)
)
)