gh-123471: Make concurrent iteration over itertools.cycle safe under free-threading - #131212
Conversation
|
The current implementation of
cpython/Modules/itertoolsmodule.c Lines 1222 to 1225 in faa80fc
cpython/Modules/itertoolsmodule.c Lines 1218 to 1219 in faa80fc while another thread is still using By "safe under free-threading" I mean the free-threading build will not crash. No guarantees are given about any "correctness" of the results (I can adapt the title and news entry if desired). It is true that using the planned |
|
Okay, this sounds reasonable :-) Thanks for explaining the purpose of the edit. Please do check that performance hasn't been negatively impacted (if so, the make an |
|
@rhettinger In the benchmarks I did this PR is faster for the normal build. There are two reasons for this
The gain from this PR is small though (and I would not be surprised if for different platforms or benchmark tests the gain is zero). Here are the results of one of the benchmarks I did: Benchmark codeScript to generate and plot the data |
…e under free-threading (python#131212) Co-authored-by: Kumar Aditya <kumaraditya@python.org>
…e under free-threading (python#131212) Co-authored-by: Kumar Aditya <kumaraditya@python.org>
…e under free-threading (python#131212) Co-authored-by: Kumar Aditya <kumaraditya@python.org>
…132814) (GH-135689) (GH-144402) (GH-146033) (GH-142957) (GH-153791) Combined backport of PRs from main branch, fixing free-threading data-races in itertools: * gh-123471: make concurrent iteration over `itertools.cycle` safe under free-threading (gh-131212) * gh-123471: Make itertools.product and itertools.combinations thread-safe (GH-132814) * gh-123471: Make itertools.chain thread-safe (gh-135689) * gh-123471: Make concurrent iteration over `itertools.permutations` and `itertools.combinations_with_replacement` thread-safe (gh-144402) * gh-123471: make concurrent iteration over itertools.accumulate thread-safe (gh-144486) * gh-123471: Make `itertools.zip_longest` safe in the FT build (gh-146033) (cherry picked from commit 26a1cd4) (cherry picked from commit 847d1c2) (cherry picked from commit 0533c1f) (cherry picked from commit 009c8c0) (cherry picked from commit 3a24856) (cherry picked from commit 9214e3f) Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com> Co-authored-by: Kumar Aditya <kumaraditya@python.org>


See #124397
cycle->it. Instead we usecycle->indexas a check whether the iterator has been exhausted or not.cycle->firstpassitertools.batchedin a single file.