As I was refreshing myself on DFS and BFS algorithms I was reminded of the different Python Queue types.

I wanted to go a bit deeper into them and remember what the syntax and uses of them where.

Queue

Queue is a thread safe Fifo queue


from queue import Queue


# Create queue
queue = Queue()


# Add items to Queue
queue.put(str)

# Get from queue
item = queue.get()

# Get size of the queue
q.qsize()

# Check if empty
q.empty()

LifoQueue

LifoQueue is the same as Queue but with Last in last out semantics.

This operates like a Stack

deque

collections.deque operates similarly but is a double ended queue allowing for both Lifo and Fifo.

deque also uses slightly different syntax.


from collections import deque

# Create queue
queue = deque()


# Add items to Queue
# Add on the right
queue.append(str)

# Add on the left
queue.appendleft(str)


# Get from queue
# Get from the right
item = queue.pop()

# Get from the left 
item = queue.popleft()

See Python Docs

multiprocessing.queue

multiprocessing.queue on the other hand is the same as the queue.Queue but is used for multiprocessing vs multi threading.

This is useful if you have a Compute bound application vs a IO bound one.