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 — Queue shutdown · Issue #96471 · python/cpython

Queue shutdown #96471

New issue
Copy link
New issue
Copy link
Open
Open
Queue shutdown#96471
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-multiprocessingtype-featureA feature request or enhancementA feature request or enhancement

Description

@EpicWink
EpicWink
opened on Sep 1, 2022
Issue body actions

Add a shutdown method to queue class (threading queue, multiprocessing, asyncio) which causes all future puts to raise (a queue.QueueShutdown) and all future gets once the queue is empty to also raise, unblocking all waiters. An optional argument immediate=True will skip the requirement for the queue to be empty.

This will enable producers and consumers to use the queue to know when to stop. This is important because both producers and consumers can be blocked waiting on the queue.

Previous discussion:

  • queue termination on Discourse
  • Add a shutdown method to threading and asyncio Queue classes #81515

Linked PRs

  • gh-96471: Add queue shutdown #96474
  • gh-96471: Add queue shutdown, next step. #102499
  • gh-96471: Add threading queue shutdown #104225
  • gh-96471: Add asyncio queue shutdown #104228
  • gh-96471: Add multiprocessing queue shutdown #104230
  • gh-96471: Add threading queue shutdown #104750
  • Revert "gh-96471: Add shutdown() method to queue.Queue (#104750)" #115265
  • gh-96471: Correct docs for queue shutdown raises #115838
  • gh-96471: Add ShutDown to threading module '__all__' #116699
  • gh-96471: Correct documentation for asyncio queue shutdown #117621
  • gh-96471: Add multiprocessing.Queue.shutdown() #138828

Activity

Next
  1. EpicWink
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Sep 1, 2022
  2. gvanrossum
    added
    topic-asyncio
    on Dec 1, 2022
  3. ezio-melotti
    added this to asyncioon Dec 1, 2022
  4. ezio-melotti
    moved this to Todo in asyncioon Dec 1, 2022
  5. gvanrossum commented on May 9, 2023

    @gvanrossum
    gvanrossum
    on May 9, 2023
    Member
    More actions

    I wonder if we need a more complete specification and motivation before starting to approve PRs. Just reading the docs for the asyncio PR (#104228) I have tons of questions, e.g.

    • Why do we need immediate=True? Couldn't you get the same effect by calling shutdown() and then depleting the queue by calling get() in a tight loop until it raises? Having two variants causes a fair amount of extra code. (From an early discussion on the topic it appears it's for atomicity, but I'm not too sure we had considered this solution.)
    • What should full() and empty() return when the queue is shut down?
    • Do we need a new inquiry method (is_alive()?) to tell whether a queue has been shut down?
    • When the queue is shut down and empty, should get_nowait() raise QueueShutDown or QueueEmpty?
    • How do task_done() and join() interact with shutting down?
    • What did I miss? (If there's already a spec somewhere, please link here.)
  6. EpicWink commented on May 10, 2023

    @EpicWink
    EpicWink
    on May 10, 2023
    ContributorAuthor
    More actions

    I wonder if we need a more complete specification and motivation before starting to approve PRs.

    I agree. There were some things that needed to be considered but weren't discussed in the Discourse thread. I'll open I have opened up a new discussion

    Why do we need immediate=True? Couldn't you get the same effect by calling shutdown() and then depleting the queue by calling get() in a tight loop until it raises?

    During that tight loop a consumer may finish and get a new item to process, increasing the time before all consumers have exited, especially if the queue size is large.

    • What should full() and empty() return when the queue is shut down?
    • Do we need a new inquiry method (is_alive()?) to tell whether a queue has been shut down?
    • When the queue is shut down and empty, should get_nowait() raise QueueShutDown or QueueEmpty?
    • How do task_done() and join() interact with shutting down?

    To be included in aforementioned new discussion.

  7. EpicWink commented on May 20, 2023

    @EpicWink
    EpicWink
    on May 20, 2023
    ContributorAuthor
    More actions

    Actually, now that I think about it, you could have a tight loop consuming all items for immediate=True, if:

    • the loop is in the shutdown method
    • state is set to 'shut-down` before the loop
    • the lock is held for the entire loop
    • everything is notified at the end of the loop
  8. gvanrossum commented on May 20, 2023

    @gvanrossum
    gvanrossum
    on May 20, 2023
    Member
    More actions

    So it would still need the immediate=True flag on shutdown(), but the rest of the code would not have to distinguish between shut-down and immediately-shut-down. That's much better!

    I'm not sure I follow "everything is notified at the end of the loop" -- is this about task_done()?

  9. EpicWink commented on May 22, 2023

    @EpicWink
    EpicWink
    on May 22, 2023
    ContributorAuthor
    More actions

    I'm not sure I follow "everything is notified at the end of the loop" -- is this about task_done()?

    Basically (for threading):

    self.not_empty.notify_all()
    self.not_full.notify_all()
    self.all_tasks_done.notify_all()

    So consumers (ie callers of queue.join, queue.get, and queue.put) are all unblocked

    Edit: notify -> notify_all

  10. gvanrossum commented on May 22, 2023

    @gvanrossum
    gvanrossum
    on May 22, 2023
    Member
    More actions

    Not sure if join should be unconditionally unblocked. What if there's a lagging thread that got an item from the queue before the shutdown happened and is still working on it? It will eventually call task_done().

  11. EpicWink commented on May 22, 2023

    @EpicWink
    EpicWink
    on May 22, 2023
    ContributorAuthor
    More actions

    Not sure if join should be unconditionally unblocked. What if there's a lagging thread that got an item from the queue before the shutdown happened and is still working on it? It will eventually call task_done().

    You're right, shutdown should only take away from unfinished_tasks as many as it consumes. I'll fix that later today (turns out this makes a few tests hang, investigating)

  12. EpicWink commented on Feb 8, 2024

    @EpicWink
    EpicWink
    on Feb 8, 2024
    ContributorAuthor
    More actions

    After investigation, the issue was the tests calling task_done without ever calling get, breaking my assumptions and making unfinished_tasks go negative. The solution I went with was to never make unfinished_tasks go below zero.

  13. gvanrossum
    added 2 commits that reference this issue on Feb 10, 2024

    gh-96471: Add shutdown() method to queue.Queue (#104750)

    b2d9d13

    Revert "pythongh-96471: Add shutdown() method to queue.Queue (python#…

    b431b87
  14. fsc-eriker
    added a commit that references this issue on Feb 14, 2024

    pythongh-96471: Add shutdown() method to queue.Queue (python#104750)

    4383673
  15. 10 remaining items

  16. diegorusso
    added 3 commits that reference this issue on Apr 17, 2024

    pythongh-96471: Add ShutDown to queue.py '__all__' (python#116699)

    b613d73

    pythongh-96471: Add asyncio queue shutdown (python#104228)

    21be21c

    pythongh-96471: Correct documentation for asyncio queue shutdown (pyt…

    812acf6
  17. kumaraditya303
    removed
    topic-asyncio
    on Jul 27, 2024
  18. kumaraditya303
    removed this from asyncioon Jul 27, 2024
  19. LukasWoodtli
    added a commit that references this issue on Jan 22, 2025

    pythongh-96471: Correct docs for queue shutdown (python#115838)

    12b70b5
  20. amauryfa
    added a commit that references this issue on Aug 10, 2025

    Since Python 3.13, shutdown queues raise a `QueueShutDown` exception.

    519b905
  21. maxsimov
    added a commit that references this issue on Apr 26, 2026

    build: bump requires-python to >=3.13; use stdlib Queue.shutdown()

    8e90bb9
  22. Jean-Luc-Picard-2021 commented on Aug 18, 2026

    @Jean-Luc-Picard-2021
    Jean-Luc-Picard-2021
    on Aug 18, 2026
    More actions

    Would it be possibly to add "shutdown" also to Threading.join etc..
    Basically replicating the exception signature throws InterruptedException
    from Java. Or what is the Pythonesk approach to get out of Threading.join ?

    Similarly for Semaphores. Basically extending the concept introduced
    here from Queues, to other concurrency elements such as the more
    simpler Semaphores and even Threads itself. BTW: Didn't check whether

    my feature request has a duplicate and whether this is the right place.

  23. gvanrossum commented on Aug 18, 2026

    @gvanrossum
    gvanrossum
    on Aug 18, 2026
    Member
    More actions

    @Jean-Luc-Picard-2021 Please start a discussion on Discord if you want your suggestion to be heard.

    Reacted by Laurie O
  24. Jean-Luc-Picard-2021 commented on Aug 21, 2026

    @Jean-Luc-Picard-2021
    Jean-Luc-Picard-2021
    on Aug 21, 2026
    More actions

    Thx, I have 99 problems but not getting heard is none of mine.
    Proof: See comment above from former BDFL himself.
    But it would be more helpful to hint why I should move

    to discord. Does Python not anymore use GitHub, Discourse, etc..?
    BTW: I don't have a Discord account, because of social media
    inflation, and sometimes ticket systems and not discussions are

    used for feature requests and/or roadmaps. Because of cohesion.

  25. EpicWink commented on Aug 21, 2026

    @EpicWink
    EpicWink
    on Aug 21, 2026
    ContributorAuthor
    More actions

    Does Python not anymore use GitHub

    @Jean-Luc-Picard-2021 CPython and most PSF projects use Discourse to discuss proposals. Discord is a quick way to talk to people for design suggestions before creating a Discourse topic, but in my opinion can be skipped if you have a concrete enough proposal to debate with.

  26. picnixz
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    topic-multiprocessing
    on Aug 22, 2026
  27. gpshead
    added a commit that references this issue on Nov 12, 2026

    pythongh-96471: Reimplement multiprocessing.Queue.shutdown()

    a5bf5f3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-multiprocessingtype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.