David542
David542

Reputation: 110267

Implementing a FIFO queue

In python3.7 there are two options for an off-the-shelf FIFO queue object:

However, all that the docs say about a SimpleQueue is that:

Constructor for an unbounded FIFO queue. Simple queues lack advanced functionality such as task tracking. New in version 3.7.

What would be a functional difference between then two?

Upvotes: 0

Views: 463

Answers (1)

ShadowRanger
ShadowRanger

Reputation: 155448

Literally what the docs you quoted tell you (the details can be gleaned by comparing the available methods):

  1. SimpleQueue is always unbounded (Queue is optionally bounded); they removed the full method (because it can never be full)
  2. SimpleQueue doesn't do task tracking, so it doesn't provide task_done (for consumers to indicate a task has been completed) or join (to allow a thread to block until all items put have had a matching task_done call)

This simplifies a lot of the code by dropping support for comparatively rare scenarios (I've almost never seen anyone use task_done or join, and when they do, it's often buggy, with exceptions potentially bypassing a task_done, making join block forever), improving performance a bit by side-effect.

Side-note: If you don't actually need the blocking features at all, you can skip the queue module and just use collections.deque. It's still thread-safe and atomic for appends and pops from either end, it just doesn't provide the ability to do a blocking get when the deque is empty, in exchange for being much lower overhead (implemented entirely in C, with no supplementary locking like queue classes).

Upvotes: 4

Related Questions