Skip to content

Commit dbb33bd

Browse files
committed
refactor code based on review
1 parent af909ce commit dbb33bd

File tree

1 file changed

+29
-117
lines changed

1 file changed

+29
-117
lines changed

Lib/test/test_free_threading/test_itertools.py

Lines changed: 29 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,59 @@
11
import unittest
2-
from threading import Thread, Barrier
32
from itertools import batched, chain, combinations_with_replacement, cycle, permutations
43
from test.support import threading_helper
54

65

76
threading_helper.requires_working_threading(module=True)
87

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
238

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
3115

32-
with threading_helper.start_threads(worker_threads):
33-
pass
3416

35-
barrier.reset()
17+
class ItertoolsThreading(unittest.TestCase):
3618

3719
@threading_helper.reap_threads
38-
def test_cycle(self):
39-
number_of_threads = 6
20+
def test_batched(self):
4021
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])
4225

43-
barrier = Barrier(number_of_threads)
26+
@threading_helper.reap_threads
27+
def test_cycle(self):
4428
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)
5931

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])
6436

6537
@threading_helper.reap_threads
6638
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])
9143

9244
@threading_helper.reap_threads
9345
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
10747
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])
11850

11951
@threading_helper.reap_threads
12052
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
13454
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])
14557

14658

14759
if __name__ == "__main__":

0 commit comments

Comments
 (0)