-
-
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
base: main
Are you sure you want to change the base?
Changes from all commits
3494396
68f95f9
0c06c5b
72a52de
4a0bd14
fda5521
4cb654c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 ?
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. An 'empty queue' test, when |
||
| close_queue(q) | ||
|
|
||
| @classmethod | ||
| def _test_task_done(cls, q): | ||
| for obj in iter(q.get, None): | ||
|
|
||
| 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`. |
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 ?