# queue.Queue - Adding Cancelling support

**URL:** <https://discuss.python.org/t/queue-queue-adding-cancelling-support/25123>\
**Category:** Ideas\
**Created:** [March 23, 2023, 11:21pm UTC](https://discuss.python.org/t/queue-queue-adding-cancelling-support/25123 "2023-03-23T23:21:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mementum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mementum/32/11696_2.png) [@mementum](https://discuss.python.org/u/mementum)\
**Post date:** [March 23, 2023, 11:21pm UTC](https://discuss.python.org/t/queue-queue-adding-cancelling-support/25123/1 "2023-03-23T23:21:44Z")

</div>

# Feature or enhancement

Adding methods `cancel` and `cancel_all` (akin to `notify` and `notify_all` to `queue.Queue` to support cancelling a `get` (and potentially a `put`) operation

# Pitch

Awaiting on `get` of an `asyncio` can be cancelled. But waiting on the `get` of a `queue.Queue` cannot be cancelled. A usual pattern is sending `None` to the queue, but this is simply a convention and introduces the restriction that `None` stops being a valid value returned by `get`

This would be implemented by means of a `threading.Semaphore` which would be `release`d by the method `cancel(n=1)`. A waiting `get` would check if the `Semaphore` can be acquired (non-blocking check) and if `True` it would raise a (new) `queue.Cancelled` exception.

---

<div class="post-metadata">

**Author:** ![mementum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mementum/32/11696_2.png) [@mementum](https://discuss.python.org/u/mementum)\
**Post date:** [March 23, 2023, 11:43pm UTC](https://discuss.python.org/t/queue-queue-adding-cancelling-support/25123/2 "2023-03-23T23:43:35Z")

</div>

Sample code has been added to GitHub: [queue.Queue - Adding Cancelling Support · Issue #102983 · python/cpython · GitHub](https://github.com/python/cpython/issues/102983)

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [March 24, 2023, 12:06am UTC](https://discuss.python.org/t/queue-queue-adding-cancelling-support/25123/3 "2023-03-24T00:06:49Z")

</div>

If None is a required value in the queue you can use a different sentential.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [March 24, 2023, 12:46am UTC](https://discuss.python.org/t/queue-queue-adding-cancelling-support/25123/4 "2023-03-24T00:46:30Z")

</div>

Your link doesn’t go to GitHub, it goes back to this very thread.

Are you looking at the `queue.Queue` class or `asyncio.queues.Queue`? If you need cancellable coroutines, you probably want to be looking at the latter.

---

<div class="post-metadata">

**Author:** ![EpicWink](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/epicwink/32/17968_2.png) [@EpicWink](https://discuss.python.org/u/EpicWink)\
**Post date:** [March 24, 2023, 4:16am UTC](https://discuss.python.org/t/queue-queue-adding-cancelling-support/25123/5 "2023-03-24T04:16:50Z")

</div>

Related: [Queue termination](https://discuss.python.org/t/queue-termination/18386) (the entire queue is cancelled here, including all producers and consumers, instead of just one producer/consumer).

---

<div class="post-metadata">

**Author:** ![mementum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mementum/32/11696_2.png) [@mementum](https://discuss.python.org/u/mementum)\
**Post date:** [March 24, 2023, 8:05am UTC](https://discuss.python.org/t/queue-queue-adding-cancelling-support/25123/6 "2023-03-24T08:05:00Z")

</div>

The point is to keep the values of the `queue.Queue` free from conventions.

---

<div class="post-metadata">

**Author:** ![mementum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mementum/32/11696_2.png) [@mementum](https://discuss.python.org/u/mementum)\
**Post date:** [March 24, 2023, 8:05am UTC](https://discuss.python.org/t/queue-queue-adding-cancelling-support/25123/7 "2023-03-24T08:05:44Z")

</div>

It does now. It was probably a combination of being tired and copy and paste, gift and poison of modern times.

---

<div class="post-metadata">

**Author:** ![mementum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mementum/32/11696_2.png) [@mementum](https://discuss.python.org/u/mementum)\
**Post date:** [March 24, 2023, 8:08am UTC](https://discuss.python.org/t/queue-queue-adding-cancelling-support/25123/8 "2023-03-24T08:08:04Z")

</div>

This is not about “terminating” the `queue.Queue` but “waking up” with proper signaling (an `Exception`) the queued `get` calls.

---

<div class="post-metadata">

**Author:** ![mementum](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mementum/32/11696_2.png) [@mementum](https://discuss.python.org/u/mementum)\
**Post date:** [March 24, 2023, 9:22am UTC](https://discuss.python.org/t/queue-queue-adding-cancelling-support/25123/9 "2023-03-24T09:22:20Z")

</div>

Proposed PR: [gh-102983: queue.Queue - Adding Cancelling Support by mementum · Pull Request #102996 · python/cpython · GitHub](https://github.com/python/cpython/pull/102996)
