Python queue Module

In Python,queuethe module provides a thread-safe queue implementation for safely passing data in multithreaded programming.

A queue is a first-in, first-out (FIFO) data structure,queuethe module provides multiple queue types, includingQueue、LifoQueueandPriorityQueue, to meet different needs.


Queue Types

1. Queue

QueueYesqueueThe most commonly used queue type in the module, which implements a standard FIFO (first-in, first-out) queue. The following isQueuethe basic usage:

Example

import queue

# Create a queue
q = queue.Queue()

# Add elements to the queue
q.put(1)
q.put(2)
q.put(3)

# Get elements from the queue
print(q.get())  # Output: 1
print(q.get())  # Output: 2
print(q.get())  # Output: 3

2. LifoQueue

LifoQueueis a LIFO (last-in, first-out) queue, similar to a stack. The following isLifoQueuethe basic usage:

Example

import queue

# Create a LIFO queue
q = queue.LifoQueue()

# Add elements to the queue
q.put(1)
q.put(2)
q.put(3)

# Get elements from the queue
print(q.get())  # Output: 3
print(q.get())  # Output: 2
print(q.get())  # Output: 1

3. PriorityQueue

PriorityQueueis a priority queue where elements are taken out in priority order. The following isPriorityQueuethe basic usage:

Example

import queue

# Create a priority queue
q = queue.PriorityQueue()

# Add elements to the queue, elements are tuples (priority, data)
q.put((3, 'Low priority'))
q.put((1, 'High priority'))
q.put((2, 'Medium priority'))

# Get elements from the queue
print(q.get())  # Output: (1, 'High priority')
print(q.get())  # Output: (2, 'Medium priority')
print(q.get())  # Output: (3, 'Low priority')

Common Methods

1. put(item, block=True, timeout=None)

willitemPut into the queue. IfblockisTrueand the queue is full, waittimeoutseconds until there is free space in the queue. IftimeoutisNone, then wait indefinitely.

2. get(block=True, timeout=None)

Get and remove an element from the queue. IfblockisTrueand the queue is empty, waittimeoutseconds until there is an element in the queue. IftimeoutisNone, then wait indefinitely.

3. qsize()

Return the number of elements in the queue.

4. empty()

If the queue is empty, returnTrue, otherwise returnFalse。

5. full()

If the queue is full, returnTrue, otherwise returnFalse。


Thread Safety

queueAll queue types of the module are thread-safe, meaning multiple threads can safely operate on the same queue at the same time without additional synchronization mechanisms. This makesqueuethe module an ideal choice for passing data in multithreaded programming.


Example: Multithreaded Queue

The following is an example of usingQueueto pass data between multiple threads:

Example

import queue
import threading
import time

# Create a queue
q = queue.Queue()

# Producer thread
def producer():
    for i in range(5):
        print(f'Produce {i}')
        q.put(i)
        time.sleep(1)

# Consumer thread
def consumer():
    while True:
        item = q.get()
        if item is None:
            break
        print(f'Consume {item}')
        q.task_done()

# Start the producer thread
producer_thread = threading.Thread(target=producer)
producer_thread.start()

# Start the consumer thread
consumer_thread = threading.Thread(target=consumer)
consumer_thread.start()

# Wait for the producer thread to finish
producer_thread.join()

# Wait for all tasks in the queue to complete
q.join()

# Send an end signal
q.put(None)
consumer_thread.join()

Common Attributes and Methods

The following is a table description of the common classes, methods, and attributes of the Python queue module (thread-safe queue), including function descriptions and examples:

queue Module Core Classes

ClassDescriptionApplicable Scenarios
queue.QueueFIFO (first-in, first-out) queueGeneral task queue
queue.LifoQueueLIFO (last-in, first-out) queue (similar to a stack)Scenarios requiring last-in, first-out
queue.PriorityQueuePriority queue (min-heap implementation)Process tasks by priority
queue.SimpleQueueSimpler FIFO queue (Python 3.7+)Scenarios not requiring advanced features

Common Methods (supported by all queue classes)

MethodDescriptionExampleReturn Value
put(item)Put an elementq.put("task1")None
get()Get and remove an elementitem = q.get()Queue element
empty()Check whether the queue is emptyif q.empty():True/False
full()Check whether the queue is fullif q.full():True/False
qsize()Return the current size of the queuesize = q.qsize()Integer
task_done()Mark task completion (used forjoin())q.task_done()None
join()blocking until all tasks are completedq.join()None

Blocking Control Parameters

ParameterDescriptionDefault ValueExample
blockWhether to block when the queue is empty/fullTrueq.get(block=False)
timeoutBlocking timeout (seconds)Noneq.put(x, timeout=5)

PriorityQueue Specific Usage

Element format:(priority, data), the smaller the priority, the earlier it is dequeued

Example

pq = queue.PriorityQueue()
pq.put((1, "low"))
pq.put((0, "high"))
print(pq.get()[1])  # Output: "high"

Example

Producer-consumer model:

Example

import queue, threading

q = queue.Queue(maxsize=3)  # Queue with capacity 3

def producer():
    for i in range(5):
        q.put(f"Task-{i}")
        print(f"Produced: Task-{i}")

def consumer():
    while True:
        item = q.get()
        print(f"Consumed: {item}")
        q.task_done()

threading.Thread(target=producer, daemon=True).start()
threading.Thread(target=consumer, daemon=True).start()
q.join()  # Wait for all tasks to complete

Priority task processing:

Example

pq = queue.PriorityQueue()
pq.put((3, "Scan"))
pq.put((1, "Emergency"))
pq.put((2, "Log"))

while not pq.empty():
    print(pq.get()[1])  # Output order: Emergency → Log → Scan

Non-blocking get (to avoid deadlock):

Example

try:
    item = q.get_nowait()  # Equivalent to q.get(block=False)
except queue.Empty:
    print("Queue is empty")
Other Extensions