mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
gh-123471: make concurrent iteration over itertools.cycle
safe under free-threading (#131212)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
This commit is contained in:
parent
b6237c3602
commit
26a1cd4e8c
3 changed files with 46 additions and 14 deletions
64
Lib/test/test_free_threading/test_itertools.py
Normal file
64
Lib/test/test_free_threading/test_itertools.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
import unittest
|
||||
from threading import Thread, Barrier
|
||||
from itertools import batched, cycle
|
||||
from test.support import threading_helper
|
||||
|
||||
|
||||
threading_helper.requires_working_threading(module=True)
|
||||
|
||||
class ItertoolsThreading(unittest.TestCase):
|
||||
|
||||
@threading_helper.reap_threads
|
||||
def test_batched(self):
|
||||
number_of_threads = 10
|
||||
number_of_iterations = 20
|
||||
barrier = Barrier(number_of_threads)
|
||||
def work(it):
|
||||
barrier.wait()
|
||||
while True:
|
||||
try:
|
||||
_ = next(it)
|
||||
except StopIteration:
|
||||
break
|
||||
|
||||
data = tuple(range(1000))
|
||||
for it in range(number_of_iterations):
|
||||
batch_iterator = batched(data, 2)
|
||||
worker_threads = []
|
||||
for ii in range(number_of_threads):
|
||||
worker_threads.append(
|
||||
Thread(target=work, args=[batch_iterator]))
|
||||
|
||||
with threading_helper.start_threads(worker_threads):
|
||||
pass
|
||||
|
||||
barrier.reset()
|
||||
|
||||
@threading_helper.reap_threads
|
||||
def test_cycle(self):
|
||||
number_of_threads = 6
|
||||
number_of_iterations = 10
|
||||
number_of_cycles = 400
|
||||
|
||||
barrier = Barrier(number_of_threads)
|
||||
def work(it):
|
||||
barrier.wait()
|
||||
for _ in range(number_of_cycles):
|
||||
_ = next(it)
|
||||
|
||||
data = (1, 2, 3, 4)
|
||||
for it in range(number_of_iterations):
|
||||
cycle_iterator = cycle(data)
|
||||
worker_threads = []
|
||||
for ii in range(number_of_threads):
|
||||
worker_threads.append(
|
||||
Thread(target=work, args=[cycle_iterator]))
|
||||
|
||||
with threading_helper.start_threads(worker_threads):
|
||||
pass
|
||||
|
||||
barrier.reset()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Loading…
Add table
Add a link
Reference in a new issue