Skip to content

gh-124397: Add threading.iter_locked #133908

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

eendebakpt
Copy link
Contributor

@eendebakpt eendebakpt commented May 11, 2025

@ZeroIntensity
Copy link
Member

@serhiy-storchaka, what was the rationale behind putting this in the threading module? I'm not against it, but I'm more in favor of itertools because threading doesn't expose any other utilities like this. Maybe we should add a threading.util module?


The ``iter_locked`` makes non-atomic iterators atomic::

class non_atomic_iterator:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure non_atomic_iterator is a good example of something that exists in the wild, because it's not thread-safe on GIL-ful builds either. The thread can arbitrarily switch during execution of Python code, so this would need a lock anyway if concurrent iteration were a use case. For free-threading, __next__ itself should have its own locking to prevent races. How was this design reached?

@serhiy-storchaka
Copy link
Member

  1. It depends on the threading lock.
  2. There will be a different implementation in the asyncio module that uses an asyncio lock.

@ZeroIntensity
Copy link
Member

  1. It seems reasonable to add a lock_type parameter for different types of locking, e.g. Lock vs RLock.
  2. Why can't there be itertools.serialize and then asyncio.async_serialize? asyncio reimplements plenty of things, so it wouldn't be particularly inconsistent/confusing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants