|
1 | 1 | import unittest |
2 | | -from threading import Thread, Barrier |
3 | 2 | from itertools import batched, chain, combinations_with_replacement, cycle, permutations |
4 | 3 | from test.support import threading_helper |
5 | 4 |
|
6 | 5 |
|
7 | 6 | threading_helper.requires_working_threading(module=True) |
8 | 7 |
|
9 | | -class ItertoolsThreading(unittest.TestCase): |
10 | | - |
11 | | - @threading_helper.reap_threads |
12 | | - def test_batched(self): |
13 | | - number_of_threads = 10 |
14 | | - number_of_iterations = 20 |
15 | | - barrier = Barrier(number_of_threads) |
16 | | - def work(it): |
17 | | - barrier.wait() |
18 | | - while True: |
19 | | - try: |
20 | | - next(it) |
21 | | - except StopIteration: |
22 | | - break |
23 | 8 |
|
24 | | - data = tuple(range(1000)) |
25 | | - for it in range(number_of_iterations): |
26 | | - batch_iterator = batched(data, 2) |
27 | | - worker_threads = [] |
28 | | - for ii in range(number_of_threads): |
29 | | - worker_threads.append( |
30 | | - Thread(target=work, args=[batch_iterator])) |
| 9 | +def work_iterator(it): |
| 10 | + while True: |
| 11 | + try: |
| 12 | + next(it) |
| 13 | + except StopIteration: |
| 14 | + break |
31 | 15 |
|
32 | | - with threading_helper.start_threads(worker_threads): |
33 | | - pass |
34 | 16 |
|
35 | | - barrier.reset() |
| 17 | +class ItertoolsThreading(unittest.TestCase): |
36 | 18 |
|
37 | 19 | @threading_helper.reap_threads |
38 | | - def test_cycle(self): |
39 | | - number_of_threads = 6 |
| 20 | + def test_batched(self): |
40 | 21 | number_of_iterations = 10 |
41 | | - number_of_cycles = 400 |
| 22 | + for _ in range(number_of_iterations): |
| 23 | + it = batched(tuple(range(1000)), 2) |
| 24 | + threading_helper.run_concurrently(work_iterator, nthreads=10, args=[it]) |
42 | 25 |
|
43 | | - barrier = Barrier(number_of_threads) |
| 26 | + @threading_helper.reap_threads |
| 27 | + def test_cycle(self): |
44 | 28 | def work(it): |
45 | | - barrier.wait() |
46 | | - for _ in range(number_of_cycles): |
47 | | - try: |
48 | | - next(it) |
49 | | - except StopIteration: |
50 | | - pass |
51 | | - |
52 | | - data = (1, 2, 3, 4) |
53 | | - for it in range(number_of_iterations): |
54 | | - cycle_iterator = cycle(data) |
55 | | - worker_threads = [] |
56 | | - for ii in range(number_of_threads): |
57 | | - worker_threads.append( |
58 | | - Thread(target=work, args=[cycle_iterator])) |
| 29 | + for _ in range(400): |
| 30 | + next(it) |
59 | 31 |
|
60 | | - with threading_helper.start_threads(worker_threads): |
61 | | - pass |
62 | | - |
63 | | - barrier.reset() |
| 32 | + number_of_iterations = 6 |
| 33 | + for _ in range(number_of_iterations): |
| 34 | + it = cycle((1, 2, 3, 4)) |
| 35 | + threading_helper.run_concurrently(work, nthreads=6, args=[it]) |
64 | 36 |
|
65 | 37 | @threading_helper.reap_threads |
66 | 38 | def test_chain(self): |
67 | | - number_of_threads = 6 |
68 | | - number_of_iterations = 20 |
69 | | - |
70 | | - barrier = Barrier(number_of_threads) |
71 | | - def work(it): |
72 | | - barrier.wait() |
73 | | - while True: |
74 | | - try: |
75 | | - next(it) |
76 | | - except StopIteration: |
77 | | - break |
78 | | - |
79 | | - data = [(1, )] * 200 |
80 | | - for it in range(number_of_iterations): |
81 | | - chain_iterator = chain(*data) |
82 | | - worker_threads = [] |
83 | | - for ii in range(number_of_threads): |
84 | | - worker_threads.append( |
85 | | - Thread(target=work, args=[chain_iterator])) |
86 | | - |
87 | | - with threading_helper.start_threads(worker_threads): |
88 | | - pass |
89 | | - |
90 | | - barrier.reset() |
| 39 | + number_of_iterations = 10 |
| 40 | + for _ in range(number_of_iterations): |
| 41 | + it = chain(*[(1,)] * 200) |
| 42 | + threading_helper.run_concurrently(work_iterator, nthreads=6, args=[it]) |
91 | 43 |
|
92 | 44 | @threading_helper.reap_threads |
93 | 45 | def test_combinations_with_replacement(self): |
94 | | - number_of_threads = 6 |
95 | | - number_of_iterations = 36 |
96 | | - data = tuple(range(2)) |
97 | | - |
98 | | - barrier = Barrier(number_of_threads) |
99 | | - def work(it): |
100 | | - barrier.wait() |
101 | | - while True: |
102 | | - try: |
103 | | - next(it) |
104 | | - except StopIteration: |
105 | | - break |
106 | | - |
| 46 | + number_of_iterations = 6 |
107 | 47 | for _ in range(number_of_iterations): |
108 | | - cwr_iterator = combinations_with_replacement(data, 2) |
109 | | - worker_threads = [] |
110 | | - for _ in range(number_of_threads): |
111 | | - worker_threads.append( |
112 | | - Thread(target=work, args=[cwr_iterator])) |
113 | | - |
114 | | - with threading_helper.start_threads(worker_threads): |
115 | | - pass |
116 | | - |
117 | | - barrier.reset() |
| 48 | + it = combinations_with_replacement(tuple(range(2)), 2) |
| 49 | + threading_helper.run_concurrently(work_iterator, nthreads=6, args=[it]) |
118 | 50 |
|
119 | 51 | @threading_helper.reap_threads |
120 | 52 | def test_permutations(self): |
121 | | - number_of_threads = 6 |
122 | | - number_of_iterations = 36 |
123 | | - data = tuple(range(4)) |
124 | | - |
125 | | - barrier = Barrier(number_of_threads) |
126 | | - def work(it): |
127 | | - barrier.wait() |
128 | | - while True: |
129 | | - try: |
130 | | - next(it) |
131 | | - except StopIteration: |
132 | | - break |
133 | | - |
| 53 | + number_of_iterations = 6 |
134 | 54 | for _ in range(number_of_iterations): |
135 | | - perm_iterator = permutations(data, 2) |
136 | | - worker_threads = [] |
137 | | - for _ in range(number_of_threads): |
138 | | - worker_threads.append( |
139 | | - Thread(target=work, args=[perm_iterator])) |
140 | | - |
141 | | - with threading_helper.start_threads(worker_threads): |
142 | | - pass |
143 | | - |
144 | | - barrier.reset() |
| 55 | + it = permutations(tuple(range(4)), 2) |
| 56 | + threading_helper.run_concurrently(work_iterator, nthreads=6, args=[it]) |
145 | 57 |
|
146 | 58 |
|
147 | 59 | if __name__ == "__main__": |
|
0 commit comments