Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-123471: Make concurrent iteration over itertools.cycle safe under free-threading#131212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Conversation
eendebakpt commented Mar 13, 2025 • edited by bedevere-app bot
Loading Uh oh!
There was an error while loading. Please reload this page.
edited by bedevere-app bot
Uh oh!
There was an error while loading. Please reload this page.
rhettinger commented Mar 14, 2025
In the "Strategy for Free Threading" umbrella issue, principle 3 is:
Given that plan, can you please articulate reason for this PR. Is there currently a risk of a crash? Also what is specifically is meant by "safe under free-threading"? AFAICT there is no concurrent access use case for cycle() that wouldn't be met by the planned options to either wrap with The itertools code was highly refined and micro-optimized with painful tooling (valgrind, vtune, etc), so I'm a bit reluctant to change long stable code unless it is really necessary. Also, I was waiting to see how the essential builtin functions/classes evolved so that itertools could follow a proven model rather than being on the bleeding edge where misadventures might persist for a long time before being noticed. |
eendebakpt commented Mar 16, 2025
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 |
rhettinger commented Mar 17, 2025 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
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 |
eendebakpt commented Mar 23, 2025
@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 |
Uh oh!
There was an error while loading. Please reload this page.
26a1cd4 into python:mainUh oh!
There was an error while loading. Please reload this page.
bedevere-bot commented Jun 2, 2025
|
…e under free-threading (python#131212) Co-authored-by: Kumar Aditya <[email protected]>
…e under free-threading (python#131212) Co-authored-by: Kumar Aditya <[email protected]>
…e under free-threading (python#131212) Co-authored-by: Kumar Aditya <[email protected]>

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