-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
gh-142837: Use semaphore accounting for multiprocessing.Queue.empty() #144832
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
Open
iamrajhans
wants to merge
7
commits into
python:main
Choose a base branch
from
iamrajhans:gh-142837-mp-queue-empty-semaphore
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+66
−6
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3494396
gh-142837: Use semaphore accounting for multiprocessing.Queue.empty()
iamrajhans 68f95f9
gh-142837: Stabilize multiprocessing Queue.get_nowait() test timing
iamrajhans 0c06c5b
gh-142837: Add NEWS entry for multiprocessing.Queue.empty() fix
iamrajhans 72a52de
Merge branch 'main' into gh-142837-mp-queue-empty-semaphore
iamrajhans 4a0bd14
Merge branch 'main' into gh-142837-mp-queue-empty-semaphore
iamrajhans fda5521
Merge branch 'main' into gh-142837-mp-queue-empty-semaphore
iamrajhans 4cb654c
Merge branch 'main' into gh-142837-mp-queue-empty-semaphore
iamrajhans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1235,7 +1235,18 @@ def test_get(self): | |
| break | ||
| self.assertEqual(queue_empty(queue), False) | ||
|
|
||
| self.assertEqual(queue.get_nowait(), 1) | ||
| for _ in support.sleeping_retry(support.SHORT_TIMEOUT): | ||
| try: | ||
| value = queue.get_nowait() | ||
| except pyqueue.Empty: | ||
| # Queue.empty() may become false before the feeder thread | ||
| # flushes objects to the pipe. | ||
| continue | ||
| else: | ||
| break | ||
| else: | ||
| self.fail("queue.get_nowait() unexpectedly raised Empty") | ||
| self.assertEqual(value, 1) | ||
| self.assertEqual(queue.get(True, None), 2) | ||
| self.assertEqual(queue.get(True), 3) | ||
| self.assertEqual(queue.get(timeout=1), 4) | ||
|
|
@@ -1320,6 +1331,29 @@ def test_qsize(self): | |
| self.assertEqual(q.qsize(), 0) | ||
| close_queue(q) | ||
|
|
||
| def test_empty_uses_semaphore_count(self): | ||
| if self.TYPE != 'processes': | ||
| self.skipTest(f'test not appropriate for {self.TYPE}') | ||
|
|
||
| q = self.Queue() | ||
| try: | ||
| q._sem.get_value() | ||
| except NotImplementedError: | ||
| close_queue(q) | ||
| self.skipTest('sem_getvalue not implemented on this platform') | ||
|
|
||
| q.put('sentinel') | ||
| original_poll = q._poll | ||
| q._poll = lambda timeout=0.0: False | ||
| try: | ||
| self.assertFalse(q.empty()) | ||
| finally: | ||
| q._poll = original_poll | ||
|
|
||
| self.assertEqual(q.get(timeout=support.SHORT_TIMEOUT), 'sentinel') | ||
| self.assertTrue(q.empty()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe adding a test when queue is empty which looks like your first one just above ? |
||
| close_queue(q) | ||
|
|
||
| @classmethod | ||
| def _test_task_done(cls, q): | ||
| for obj in iter(q.get, None): | ||
|
|
||
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Library/2026-02-16-11-20-00.gh-issue-142837.xP4kLm.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Fixed :meth:`multiprocessing.Queue.empty` to use semaphore-based size | ||
| accounting on platforms that support ``sem_getvalue()``, making its behavior | ||
| consistent with :meth:`multiprocessing.Queue.qsize` and | ||
| :meth:`multiprocessing.Queue.full`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if
tryandfinallyare very useful here ?