gh-124397: Add free-threading support for iterators.#148894
gh-124397: Add free-threading support for iterators.#148894rhettinger wants to merge 10 commits intopython:mainfrom
Conversation
21fca13 to
4c2bad0
Compare
|
|
||
| import threading | ||
|
|
||
| source = range(5) |
There was a problem hiding this comment.
This doesn't seem like a very compelling example because range() you can iterate over range concurrent from multiple threads already.
Maybe something like a simple generator would be more useful as an example?
|
|
||
| .. doctest:: | ||
|
|
||
| import threading |
There was a problem hiding this comment.
Needs >>> for doctest to run. Alternatively, use .. code-block:: python
| self.iterator = iter(iterable) | ||
| self.lock = Lock() |
There was a problem hiding this comment.
Please prefix .iterator and .lock with _ so that they aren't considered part of serialize's public API.
There was a problem hiding this comment.
Consider adding a test that an iterator __next__ raising an exception behaves properly.
|
|
||
| ## Synchronization tools for iterators ##################### | ||
|
|
||
| class serialize: |
There was a problem hiding this comment.
Should this be a subclass of Iterable? (suggested by @serhiy-storchaka on the issue)
There was a problem hiding this comment.
I don't think so. We don't do that for most iterables. Also, it isn't necessary. The ABC doesn't add any mixin methods and isinstance(serialized_iterator, Iterable) already returns True.
| with threading_helper.wait_threads_exit(): | ||
| for worker in workers: | ||
| worker.start() | ||
| start.set() |
There was a problem hiding this comment.
This doesn't ensure all threads are started before they are released from wait(). A barrier, rather than an event, should be preferred in these scenarios.
Documentation build overview
13 files changed ·
|
📚 Documentation preview 📚: https://cpython-previews--148894.org.readthedocs.build/