Fix a recent bug in executors. Thread executor needs to return
[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         f = compute_factorial_thread(_)
41         results.append(f)
42     smart_future.wait_all(results)
43     for future in results:
44         print(f'Thread: {future}')
45     texecutor = executors.DefaultExecutors().thread_pool()
46     texecutor.shutdown()
47
48
49 @decorator_utils.timed
50 def test_process_parallelization() -> None:
51     results = []
52     for _ in range(50):
53         results.append(compute_factorial_process(_))
54     for future in smart_future.wait_any(results):
55         print(f'Process: {future}')
56     pexecutor = executors.DefaultExecutors().process_pool()
57     pexecutor.shutdown()
58
59
60 @decorator_utils.timed
61 def test_remote_parallelization() -> None:
62     results = []
63     for _ in range(50):
64         results.append(compute_factorial_remote(_))
65     for result in smart_future.wait_any(results):
66         print(result)
67     rexecutor = executors.DefaultExecutors().remote_pool()
68     rexecutor.shutdown()
69
70
71 @bootstrap.initialize
72 def main() -> None:
73     test_thread_parallelization()
74     test_process_parallelization()
75     test_remote_parallelization()
76     sys.exit(0)
77
78
79 if __name__ == '__main__':
80     main()