409b8dc174078b1c5810098e290c042a1e9f38c3
[python_utils.git] / tests / parallelize_itest.py
1 #!/usr/bin/env python3
2
3 import logging
4 import sys
5
6 import bootstrap
7 import decorator_utils
8 import executors
9 import parallelize as p
10 import smart_future
11
12 logger = logging.getLogger(__name__)
13
14
15 @p.parallelize(method=p.Method.THREAD)
16 def compute_factorial_thread(n):
17     total = 1
18     for x in range(2, n):
19         total *= x
20     return total
21
22
23 @p.parallelize(method=p.Method.PROCESS)
24 def compute_factorial_process(n):
25     total = 1
26     for x in range(2, n):
27         total *= x
28     return total
29
30
31 @p.parallelize(method=p.Method.REMOTE)
32 def compute_factorial_remote(n):
33     total = 1
34     for x in range(2, n):
35         total *= x
36     return total
37
38
39 @decorator_utils.timed
40 def test_thread_parallelization() -> None:
41     results = []
42     for _ in range(50):
43         f = compute_factorial_thread(_)
44         results.append(f)
45     smart_future.wait_all(results)
46     for future in results:
47         print(f'Thread: {future}')
48     texecutor = executors.DefaultExecutors().thread_pool()
49     texecutor.shutdown()
50
51
52 @decorator_utils.timed
53 def test_process_parallelization() -> None:
54     results = []
55     for _ in range(50):
56         results.append(compute_factorial_process(_))
57     for future in smart_future.wait_any(results):
58         print(f'Process: {future}')
59     pexecutor = executors.DefaultExecutors().process_pool()
60     pexecutor.shutdown()
61
62
63 @decorator_utils.timed
64 def test_remote_parallelization() -> None:
65     results = []
66     for _ in range(10):
67         results.append(compute_factorial_remote(_))
68     for result in smart_future.wait_any(results):
69         print(result)
70     rexecutor = executors.DefaultExecutors().remote_pool()
71     rexecutor.shutdown()
72
73
74 @bootstrap.initialize
75 def main() -> None:
76     test_thread_parallelization()
77     test_process_parallelization()
78     test_remote_parallelization()
79     sys.exit(0)
80
81
82 if __name__ == '__main__':
83     try:
84         main()
85     except Exception as e:
86         logger.exception(e)
87         sys.exit(1)