Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ccd0bbc
draft: impl lazy input consumption in mp.Pool.imap(_unordered)
Jul 20, 2025
002ef46
Use semaphore to synchronize threads
Jul 20, 2025
6e0bc58
Update buffersize behavior to match concurrent.futures.Executor behavior
Jul 21, 2025
62b2b6a
Release all `buffersize_lock` obj from the parent thread when terminate
Jul 21, 2025
0b6ba41
Add 2 basic `ThreadPool.imap()` tests w/ and w/o buffersize
Jul 21, 2025
aade15e
Fix accidental swap in imports
Jul 21, 2025
fb38a72
clear Pool._taskqueue_buffersize_semaphores safely
Jul 21, 2025
6ef488b
Slightly optimize Pool._taskqueue_buffersize_semaphores terminate
Jul 21, 2025
1716725
Rename `Pool.imap()` buffersize-related tests
Jul 21, 2025
9b43cd0
Fix typo in `IMapIterator.__init__()`
Jul 22, 2025
2d89341
Add tests for buffersize combinations with other kwargs
Jul 22, 2025
9ab2705
Remove if-branch in `_terminate_pool`
Jul 27, 2025
a955003
Add more edge-case tests for `imap` and `imap_unodered`
Jul 27, 2025
80efd6e
Split inf iterable test for `imap` and `imap_unordered`
Jul 27, 2025
83d6930
Add doc for `buffersize` argument of `imap` and `imap_unordered`
Jul 27, 2025
995ad8c
add *versionadded* for `imap_unordered`
Jul 28, 2025
3b6ad65
Remove ambiguity in `buffersize` description.
Jul 28, 2025
c941c16
Set *versionadded* as next in docs
Jul 28, 2025
d09e891
Add whatsnew entry
Jul 28, 2025
9c6d89d
Fix aggreed comments on code formatting/minor refactoring
Jul 28, 2025
4550a01
Remove `imap` and `imap_unordered` body code duplication
Jul 28, 2025
77bde4d
Merge branch 'main' into feature/add-buffersize-to-multiprocessing
obaltian Aug 31, 2025
aec39fc
Merge branch 'main' into feature/add-buffersize-to-multiprocessing
obaltian Sep 3, 2025
a469f2d
Merge remote-tracking branch 'origin/main' into feature/add-buffersiz…
obaltian Jul 17, 2026
bcda6e7
Remove extra import
obaltian Jul 17, 2026
1441eaa
Update Lib/multiprocessing/pool.py
obaltian Jul 18, 2026
6d61bd1
Update Doc/library/multiprocessing.rst
obaltian Jul 18, 2026
0e43d9c
Apply suggestion from @encukou
obaltian Jul 18, 2026
4ed84bc
Make `buffersize` argument kwarg-only
Jul 18, 2026
f7aeb2e
Use set for `._taskqueue_buffersize_semaphores`
Jul 18, 2026
78b1f58
Make `buffersize` arg check inline
Jul 18, 2026
49b03d9
Simplify `.imap()` and `.imap_undered()` tests
Jul 18, 2026
455b748
Fix iterator instantiation in in `.imap()`
Jul 18, 2026
56eaf3f
Mark kw-only arg in `imap`/`imap_unordered` in doc
Jul 18, 2026
ea540bf
Merge branch 'main' into feature/add-buffersize-to-multiprocessing
obaltian Jul 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use set for ._taskqueue_buffersize_semaphores
  • Loading branch information
Sasha Baltian
Sasha Baltian committed Jul 18, 2026
commit f7aeb2eeb68cabf235e337172478e4af151e3f6b
16 changes: 6 additions & 10 deletions Lib/multiprocessing/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ def __init__(self, processes=None, initializer=None, initargs=(),
self._taskqueue = queue.SimpleQueue()
# The _taskqueue_buffersize_semaphores exist to allow calling .release()
# on every active semaphore when the pool is terminating to let task_handler
# wake up to stop. It's a dict so that each iterator object can efficiently
# wake up to stop. It's a set so that each iterator object can efficiently
# deregister its semaphore when iterator finishes.
self._taskqueue_buffersize_semaphores = {}
self._taskqueue_buffersize_semaphores = set()
# The _change_notifier queue exist to wake up self._handle_workers()
# when the cache (self._cache) is empty or when there is a change in
# the _state variable of the thread that runs _handle_workers.
Expand Down Expand Up @@ -712,11 +712,9 @@ def _terminate_pool(cls, taskqueue, inqueue, outqueue, pool, change_notifier,

task_handler._state = TERMINATE
# Release all semaphores to wake up task_handler to stop.
for job_id, buffersize_sema in tuple(
taskqueue_buffersize_semaphores.items()
):
for buffersize_sema in tuple(taskqueue_buffersize_semaphores):
buffersize_sema.release()
taskqueue_buffersize_semaphores.pop(job_id, None)
taskqueue_buffersize_semaphores.discard(buffersize_sema)

util.debug('helping task handler/workers to finish')
cls._help_stuff_finish(inqueue, task_handler, len(pool))
Expand Down Expand Up @@ -877,9 +875,7 @@ def __init__(self, pool, *, buffersize=None):
self._buffersize_sema = None
else:
self._buffersize_sema = threading.Semaphore(buffersize)
self._pool._taskqueue_buffersize_semaphores[self._job] = (
self._buffersize_sema
)
self._pool._taskqueue_buffersize_semaphores.add(self._buffersize_sema)

def __iter__(self):
return self
Expand Down Expand Up @@ -910,7 +906,7 @@ def next(self, timeout=None):
def _stop_iterator(self):
if self._pool is not None:
# `self._pool` could be set to `None` in previous `.next()` calls
self._pool._taskqueue_buffersize_semaphores.pop(self._job, None)
self._pool._taskqueue_buffersize_semaphores.discard(self._buffersize_sema)
self._pool = None
raise StopIteration from None

Expand Down