GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-96471: Add threading queue shutdown by EpicWink · Pull Request #104750 · python/cpython

Conversation

@EpicWink

@EpicWink EpicWink commented May 22, 2023 •
edited by github-actions Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Alternate implementation of #104225, where all queue items are consumed immediately in Queue.shutdown when immediate=True is passed (see the comparison for what's changed).

  • task_done and join will now not raise after calling shutdown(immediate=True), rather they'll behave as if on a finished queue (join returns, task_done raises the standard ValueError)
  • Potentially public is_shutdown boolean attribute

This PR includes and modified changes from #104225.

  • Issue: Queue shutdown #96471

📚 Documentation preview 📚: https://cpython-previews--104750.org.readthedocs.build/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
EpicWink and others added 4 commits May 6, 2023 14:34
@EpicWink
Add threading queue shutdown
cd8ceaf
* Include docs
@YvesDup @EpicWink
Fix queue shutdown
9c2971b
* Include raised exception in docstrings
* Handle queue shutdown in task_done and join
* Factor out queue-state checks and updates to methods
* Logic fixes in qsize, get and shutdown
* Don't set unfinished_tasks to 0 on immediate shutdown
* Updated tests
* Document feature added in 3.13
@blurb-it
📜🤖 Added by blurb_it.
089eb96
@EpicWink
Shut-down immediate consumes queue
ca118d7
@EpicWink
EpicWink requested a review from rhettinger as a code owner May 22, 2023 14:47
@bedevere-bot bedevere-bot added the awaiting review label May 22, 2023
@bedevere-bot bedevere-bot mentioned this pull request May 22, 2023
Queue shutdown #96471
Open
gvanrossum
gvanrossum reviewed May 22, 2023
View reviewed changes
Comment thread Lib/queue.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@rhettinger
rhettinger removed their request for review May 22, 2023 18:54
EpicWink added 2 commits February 5, 2024 11:28
@EpicWink
Update test typing
88f918d
@EpicWink
Remove queue-size tasks instead of setting to zero
ab8d975
gvanrossum
gvanrossum reviewed Feb 7, 2024
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

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.

Great progress. I mostly have some markup nits. But the tests seem to hang. Or was I just impatient?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/queue.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_queue.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2023-05-06-04-57-10.gh-issue-96471.C9wAU7.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Feb 7, 2024

Copy link
Copy Markdown
Member

Yeah, the test is definitely hanging for me. Have you figured that out yet?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Feb 7, 2024

Copy link
Copy Markdown
Member

I'd also recommend a merge. :-)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@EpicWink
Merge remote-tracking branch 'upstream/main' into threading-queue-shu…
f279f5c
…tdown-immediate-consume
@EpicWink

EpicWink commented Feb 8, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Yeah, the test is definitely hanging for me. Have you figured that out yet?

@gvanrossum It's intermittently hanging in the test test_shutdown_immediate_put_join. q.join is blocking, after shutting down the queue, because unfinished_tasks becomes -1 (when zero is required). This is because the test calls task_done before taking items from the queue, but the implementation of shutdown() reduces unfinished_tasks by the number of items in the queue.

I think the solution is to simply ensure unfinished_tasks doesn't go negative. I don't set it straight to zero because there may still be queue consumer who will successfully complete their task and call task_done, but they will get a ValueError if unfinished_tasks was immediately set to zero. There is a problem here however if some consumers still call task_done without getting items from the queue.

Perhaps it's simpler to set unfinished_tasks to zero, then skip the ValueError raise if the queue is shut down.

Edit: I just noticed that this pull request's description says that task_done should raise ValueError on a shut down queue. Hmmm.


It's also intermittently hanging in the test test_shutdown_put_join, simply because of a logic error: 2 items are put on the queue (sometimes), then it's shut down, then one task is marked as done, then the queue is waited for (aka joined on). If only one item was put on the queue, then the test simply fails during assertion.

Solution here (which fixes the assertion for test_shutdown_immediate_put_join as well) is to shut down the queue before running the put-then-join.


Edit: and of course now it's failing only in Windows (CI only succeeds because it re-runs test_queue)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

EpicWink added 4 commits February 8, 2024 20:49
@EpicWink
Improve doc wording, reference methods
971f699
@EpicWink
Reference method in news entry
ddfb8c2
@EpicWink
Remove typing in test script
3570bd8
@EpicWink
Fix join after task-done with no get
9072c6f
Also shut down before put-join in shutdown-put-join tests. Also remove indent
@EpicWink
EpicWink force-pushed the threading-queue-shutdown-immediate-consume branch from 9d0a083 to 9072c6f Compare February 8, 2024 10:50
gvanrossum
gvanrossum approved these changes Feb 8, 2024
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

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.

LG, assuming you believe the test is stable. I have one nit, let me know how you feel about that.

Oh, one more thing. If you feel like writing documentation, could you update Doc/whatsnew/3.13.rst? There's a section about modified modules. A few lines there will go a long way.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/queue.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Feb 8, 2024
EpicWink added 2 commits February 9, 2024 12:24
@EpicWink
More explicitly update 'q.unfinished_tasks'
e0927aa
@EpicWink
Add what's new entry
22adada
@EpicWink

EpicWink commented Feb 9, 2024

Copy link
Copy Markdown
Contributor Author

LG, assuming you believe the test is stable.

Test is not stable, as test_shutdown_all_methods_in_many_threads (and the corresponding immediate version) hangs intermittently in Windows. It also fails consistently when adding debugging print-statements on my machine. I'm investigating

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gvanrossum
gvanrossum approved these changes Feb 10, 2024
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

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.

Excellent! All that remains is merging it (which I will take care of), closing the alternative PR for queue.py, and then we can have another look at the asyncio and multiprocessing queues. (For the latter we still need to find a reviewer.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@gvanrossum
gvanrossum merged commit b2d9d13 into python:main Feb 10, 2024
@bedevere-app bedevere-app Bot removed the awaiting merge label Feb 10, 2024
@gvanrossum

gvanrossum commented Feb 10, 2024

Copy link
Copy Markdown
Member

@EpicWink Sorry, it looks like we have to revert this until the Windows free-threading hang has been resolved. (Although it's possible that you've uncovered a Windows-related bug in free-threading, which is a developing feature.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Feb 11, 2024

Copy link
Copy Markdown
Member

FWIW, having had only a quick look at the failing test, is it possible that it relies on timeouts too much? I see that there's a 0.1 msec delay that is used for sleeping, and _read_msg_thread() makes some non-blocking get() calls, ignoring Empty exceptions. Could there be a scenario (especially when running without GIL) where different threads run in a different order than you anticipated, and it simply doesn't read enough queue items, so the join() hangs forever?

EpicWink reacted with eyes emoji
All reactions
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this pull request Feb 14, 2024
@EpicWink @YvesDup @fsc-eriker
pythongh-96471: Add shutdown() method to queue.Queue (python#104750)
4383673
Co-authored-by: Duprat <[email protected]>
This was referenced Feb 20, 2024
gh-96471: Add asyncio queue shutdown #104228
Merged
gh-96471: Add multiprocessing queue shutdown #104230
Draft
@gvanrossum

gvanrossum commented Feb 22, 2024

Copy link
Copy Markdown
Member

From #104228 (comment): it looks like Queue.join() never raises ShutDown, contradicting the docs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@YvesDup

YvesDup commented Feb 22, 2024

Copy link
Copy Markdown
Contributor

Maybe I'm waking up a little too late, but despite having worked on the development 1 year ago, I have the impression that functionalities are missing in this merged PR.

In the initial version of the feature (#104225), the basic shutdown just forbade put operations, also released all threads blocked in put. On the other hand, it left get, join and task_done operations possible.

The immediate shutdown prohibited all possible previous operations and had to release all threads blocked in get, and join. And the queue was purged.

Currently, in the following methods (put, get, join and task_done), there is no specific behavior depending on the type of the shutdown. It's not even noted in an attribute.

I don't want to hurt anyone's feelings, but I think this PR needs to be reworked (including documentation).

PS: initial comment was done at the bad PR. Sorry

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@EpicWink EpicWink mentioned this pull request Feb 23, 2024
gh-96471: Correct docs for queue shutdown raises #115838
Merged
@EpicWink

EpicWink commented Feb 23, 2024

Copy link
Copy Markdown
Contributor Author

Maybe I'm waking up a little too late, but despite having worked on the development 1 year ago, I have the impression that functionalities are missing in this merged PR.

In the initial version of the feature (#104225), the basic shutdown just forbade put operations, also released all threads blocked in put. On the other hand, it left get, join and task_done operations possible.

The immediate shutdown prohibited all possible previous operations and had to release all threads blocked in get, and join. And the queue was purged.

Currently, in the following methods (put, get, join and task_done), there is no specific behavior depending on the type of the shutdown. It's not even noted in an attribute.

@YvesDup see comment in the issue: #96471 (comment)

Basically, the goal is to have waiters (callers of queue methods) not be blocked on a queue which has been shut-down. Making the shutdown immediate simply means gets won't take anything more from the queue (and instead except).

There is a problem with this PR though, in that q.task_done() and q.join() are documented as raising ShutDown when they don't; see #115838 for the fix for that.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@YvesDup

YvesDup commented Feb 23, 2024

Copy link
Copy Markdown
Contributor

@EpicWink , thank you for this explanation. I ve missed that :-(
Features really change from the initial version. Now it seems more efficient and simpler.
As we wrote all tests for the initial version, I 'd like to review this part because features are changed. May be we will have to remove some of tests, to modify others, or add new ones ?
Do you agree ?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@YvesDup

YvesDup commented Feb 23, 2024

Copy link
Copy Markdown
Contributor

FYI, I fixed the failling test in _read_msg_thread() method. Loop was infinite, I insert a break on queue.Shutdow exception. .... (and I removed all time.sleep())

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Feb 23, 2024

Copy link
Copy Markdown
Member

FYI, I fixed the failling test in _read_msg_thread() method. Loop was infinite, I insert a break on queue.Shutdow exception. .... (and I removed all time.sleep())

Where can we see this?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@YvesDup

YvesDup commented Feb 25, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

FYI, I fixed the failling test in _read_msg_thread() method. Loop was infinite, I insert a break on queue.Shutdow exception. .... (and I removed all time.sleep())

Where can we see this?

https://github.com/YvesDup/cpython/blob/Threading-queue-shutdown-test-failed/Lib/test/test_queue.py#L350
Debug logs r and w will be removed.

See now https://github.com/YvesDup/cpython/blob/Threading-queue-shutdown-test-failed/Lib/test/test_queue.py#L370

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Feb 25, 2024

Copy link
Copy Markdown
Member

Thanks, I'll wait for the pull request.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@YvesDup YvesDup mentioned this pull request Feb 26, 2024
gh-115258: Fix failed tests on threading queue shutdown #115940
Merged
@aisk aisk mentioned this pull request Feb 28, 2024
stdlib: Add Shutdown error and shutdown method to queue module python/typeshed#11489
Merged
AlexWaygood
AlexWaygood reviewed Feb 28, 2024
View reviewed changes
Comment thread Lib/queue.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@YvesDup

YvesDup commented Mar 12, 2024

Copy link
Copy Markdown
Contributor

Thanks, I'll wait for the pull request.

PR is ready on this issue (#115940). Sorry if I didn't understand that you wait for a PR linked to this one. Please let me know if I have to change.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Mar 12, 2024

Copy link
Copy Markdown
Member

@YvesDup, I am a little confused. There is indeed #115940, by you, but there is also gh-115898 by @EpicWink that also claims to fix these tests. How do the two relate to each other?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@YvesDup

YvesDup commented Mar 12, 2024

Copy link
Copy Markdown
Contributor

How do the two relate to each other?

There are related because there are both about on the same issue. But #115898 is only a draft.
I had informed @EpicWink that I had fixed the bug on the test, which is why I had submitted a new PR.
Maybe, should we wait for his feedback before going head ?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Mar 12, 2024

Copy link
Copy Markdown
Member

@EpicWink: We're waiting for you! ^^

YvesDup reacted with laugh emoji
All reactions
  • 😄 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@EpicWink

EpicWink commented Mar 12, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

I'll review the new PR fixing the test today.

My PR is another attempt, but unfinished. Perhaps in the future it can be added as another test.

Edit: reviewed!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@EpicWink EpicWink mentioned this pull request Mar 13, 2024
gh-96471: Add ShutDown to threading module '__all__' #116699
Merged
@YvesDup

YvesDup commented Mar 13, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

'll review the new PR fixing the test today.

My PR is another attempt, but unfinished. Perhaps in the future it can be added as another test.

Edit: reviewed!

Thank for the review.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Apr 4, 2024
Queue non-immediate shutdown doesn't unblock getters #117531
Closed
gh-117531: Unblock getters after non-immediate queue shutdown #117532
Merged
gvanrossum pushed a commit that referenced this pull request Apr 10, 2024
@EpicWink
gh-117531: Unblock getters after non-immediate queue shutdown (#117532)
6bc0b33
(This is a small tweak of the original gh-104750 which added shutdown.)
@EpicWink
EpicWink deleted the threading-queue-shutdown-immediate-consume branch April 11, 2024 01:09
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
@EpicWink @diegorusso
pythongh-117531: Unblock getters after non-immediate queue shutdown (p…
56927d4
…ython#117532)

(This is a small tweak of the original pythongh-104750 which added shutdown.)
@YvesDup YvesDup mentioned this pull request Sep 12, 2025
gh-96471: Add multiprocessing.Queue.shutdown() #138828
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gvanrossum gvanrossum gvanrossum approved these changes
@AlexWaygood AlexWaygood AlexWaygood left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@EpicWink @gvanrossum @YvesDup @AlexWaygood @bedevere-bot
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.