blob: ef154a70d92eb4a32339bc4eb5a121561878aa50 (
plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#!/usr/bin/env python3
# © Copyright 2021-2022, Scott Gasch
"""parallelize unittest."""
import logging
import sys
import bootstrap
import decorator_utils
import executors
import parallelize as p
import smart_future
logger = logging.getLogger(__name__)
@p.parallelize(method=p.Method.THREAD)
def compute_factorial_thread(n):
total = 1
for x in range(2, n):
total *= x
return total
@p.parallelize(method=p.Method.PROCESS)
def compute_factorial_process(n):
total = 1
for x in range(2, n):
total *= x
return total
@p.parallelize(method=p.Method.REMOTE)
def compute_factorial_remote(n):
total = 1
for x in range(2, n):
total *= x
return total
@decorator_utils.timed
def test_thread_parallelization() -> None:
results = []
for _ in range(50):
f = compute_factorial_thread(_)
results.append(f)
smart_future.wait_all(results)
for future in results:
print(f'Thread: {future}')
texecutor = executors.DefaultExecutors().thread_pool()
texecutor.shutdown()
@decorator_utils.timed
def test_process_parallelization() -> None:
results = []
for _ in range(50):
results.append(compute_factorial_process(_))
for future in smart_future.wait_any(results):
print(f'Process: {future}')
pexecutor = executors.DefaultExecutors().process_pool()
pexecutor.shutdown()
@decorator_utils.timed
def test_remote_parallelization() -> None:
results = []
for _ in range(10):
results.append(compute_factorial_remote(_))
for result in smart_future.wait_any(results):
print(result)
rexecutor = executors.DefaultExecutors().remote_pool()
rexecutor.shutdown()
@bootstrap.initialize
def main() -> None:
test_thread_parallelization()
test_process_parallelization()
test_remote_parallelization()
sys.exit(0)
if __name__ == '__main__':
try:
main()
except Exception as e:
logger.exception(e)
sys.exit(1)
|