mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	gh-123471: Make itertools.chain thread-safe (#135689)
This commit is contained in:
		
							parent
							
								
									536a5ff153
								
							
						
					
					
						commit
						0533c1faf2
					
				
					 3 changed files with 43 additions and 4 deletions
				
			
		| 
						 | 
				
			
			@ -1,6 +1,6 @@
 | 
			
		|||
import unittest
 | 
			
		||||
from threading import Thread, Barrier
 | 
			
		||||
from itertools import batched, cycle
 | 
			
		||||
from itertools import batched, chain, cycle
 | 
			
		||||
from test.support import threading_helper
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -17,7 +17,7 @@ def work(it):
 | 
			
		|||
            barrier.wait()
 | 
			
		||||
            while True:
 | 
			
		||||
                try:
 | 
			
		||||
                    _ = next(it)
 | 
			
		||||
                    next(it)
 | 
			
		||||
                except StopIteration:
 | 
			
		||||
                    break
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -62,6 +62,34 @@ def work(it):
 | 
			
		|||
 | 
			
		||||
            barrier.reset()
 | 
			
		||||
 | 
			
		||||
    @threading_helper.reap_threads
 | 
			
		||||
    def test_chain(self):
 | 
			
		||||
        number_of_threads = 6
 | 
			
		||||
        number_of_iterations = 20
 | 
			
		||||
 | 
			
		||||
        barrier = Barrier(number_of_threads)
 | 
			
		||||
        def work(it):
 | 
			
		||||
            barrier.wait()
 | 
			
		||||
            while True:
 | 
			
		||||
                try:
 | 
			
		||||
                    next(it)
 | 
			
		||||
                except StopIteration:
 | 
			
		||||
                    break
 | 
			
		||||
 | 
			
		||||
        data = [(1, )] * 200
 | 
			
		||||
        for it in range(number_of_iterations):
 | 
			
		||||
            chain_iterator = chain(*data)
 | 
			
		||||
            worker_threads = []
 | 
			
		||||
            for ii in range(number_of_threads):
 | 
			
		||||
                worker_threads.append(
 | 
			
		||||
                    Thread(target=work, args=[chain_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