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