queue.Queue - Adding Cancelling support

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

Sample code has been added to GitHub: queue.Queue - Adding Cancelling Support · Issue #102983 · python/cpython · GitHub

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

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.

Related: Queue termination (the entire queue is cancelled here, including all producers and consumers, instead of just one producer/consumer).

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

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

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

Proposed PR: gh-102983: queue.Queue - Adding Cancelling Support by mementum · Pull Request #102996 · python/cpython · GitHub