Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-96471: Add threading queue shutdown#104750

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

EpicWink
Copy link
Contributor

@EpicWinkEpicWink commentedMay 22, 2023
edited by github-actionsbot
Loading

Alternate implementation of#104225, where all queue items are consumed immediately inQueue.shutdown whenimmediate=True is passed (seethe comparison for what's changed).

  • task_done andjoin will now not raise after callingshutdown(immediate=True), rather they'll behave as if on a finished queue (join returns,task_done raises the standardValueError)
  • Potentially publicis_shutdown boolean attribute

This PR includes and modified changes from#104225.


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

EpicWinkand others added4 commitsMay 6, 2023 14:34
* 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
@rhettingerrhettinger removed their request for reviewMay 22, 2023 18:54
Copy link
Member

@gvanrossumgvanrossum left a 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?

@gvanrossum
Copy link
Member

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

@gvanrossum
Copy link
Member

I'd also recommend a merge. :-)

@EpicWink
Copy link
ContributorAuthor

EpicWink commentedFeb 8, 2024
edited
Loading

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

@gvanrossum It's intermittently hanging in the testtest_shutdown_immediate_put_join.q.join is blocking, after shutting down the queue, becauseunfinished_tasks becomes-1 (when zero is required). This is because the test callstask_done before taking items from the queue, but the implementation ofshutdown() reducesunfinished_tasks by the number of items in the queue.

I think the solution is to simply ensureunfinished_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 calltask_done, but they will get aValueError ifunfinished_tasks was immediately set to zero. There is a problem here however if some consumers still calltask_done without getting items from the queue.

Perhaps it's simpler to setunfinished_tasks to zero, then skip theValueError raise if the queue is shut down.

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


It's also intermittently hanging in the testtest_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 fortest_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-runstest_queue)

@EpicWinkEpicWinkforce-pushed thethreading-queue-shutdown-immediate-consume branch from9d0a083 to9072c6fCompareFebruary 8, 2024 10:50
Copy link
Member

@gvanrossumgvanrossum left a 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.

@EpicWink
Copy link
ContributorAuthor

LG, assuming you believe the test is stable.

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

Copy link
Member

@gvanrossumgvanrossum left a 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.)

@gvanrossumgvanrossum merged commitb2d9d13 intopython:mainFeb 10, 2024
@gvanrossum
Copy link
Member

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

@gvanrossum
Copy link
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-blockingget() calls, ignoringEmpty 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

fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this pull requestFeb 14, 2024
@gvanrossum
Copy link
Member

From#104228 (comment): it looks likeQueue.join() never raisesShutDown, contradicting the docs.

@YvesDup
Copy link
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 forbadeput operations, also released all threads blocked input. On the other hand, it leftget,join andtask_done operations possible.

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

Currently, in the following methods (put,get,join andtask_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

@EpicWink
Copy link
ContributorAuthor

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 forbadeput operations, also released all threads blocked input. On the other hand, it leftget,join andtask_done operations possible.

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

Currently, in the following methods (put,get,join andtask_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 thatq.task_done() andq.join() are documented as raisingShutDown when they don't; see#115838 for the fix for that.

@YvesDup
Copy link
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 ?

@YvesDup
Copy link
Contributor

FYI, I fixed thefailling test in_read_msg_thread() method. Loop was infinite, I insert abreak onqueue.Shutdow exception. .... (and I removed alltime.sleep())

@gvanrossum
Copy link
Member

FYI, I fixed thefailling test in_read_msg_thread() method. Loop was infinite, I insert abreak onqueue.Shutdow exception. .... (and I removed alltime.sleep())

Where can we see this?

@YvesDup
Copy link
Contributor

YvesDup commentedFeb 25, 2024
edited
Loading

FYI, I fixed thefailling test in_read_msg_thread() method. Loop was infinite, I insert abreak onqueue.Shutdow exception. .... (and I removed alltime.sleep())

Where can we see this?

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

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

@gvanrossum
Copy link
Member

Thanks, I'll wait for the pull request.

@YvesDup
Copy link
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.

@gvanrossum
Copy link
Member

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

@YvesDup
Copy link
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 ?

@gvanrossum
Copy link
Member

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

YvesDup reacted with laugh emoji

@EpicWink
Copy link
ContributorAuthor

EpicWink commentedMar 12, 2024
edited
Loading

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!

@YvesDup
Copy link
Contributor

YvesDup commentedMar 13, 2024
edited
Loading

'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.

gvanrossum pushed a commit that referenced this pull requestApr 10, 2024
(This is a small tweak of the originalgh-104750 which added shutdown.)
@EpicWinkEpicWink deleted the threading-queue-shutdown-immediate-consume branchApril 11, 2024 01:09
diegorusso pushed a commit to diegorusso/cpython that referenced this pull requestApr 17, 2024
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@gvanrossumgvanrossumgvanrossum approved these changes

@AlexWaygoodAlexWaygoodAlexWaygood left review comments

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

5 participants
@EpicWink@gvanrossum@YvesDup@AlexWaygood@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp