Python queue Module
In Python, the queue module provides a thread-safe queue implementation for safely passing data in multi-threaded programming.
A queue is a first-in-first-out (FIFO) data structure. The queue module provides multiple queue types, including Queue, LifoQueue, and PriorityQueue, to meet different needs.
Queue Types
Section titled “Queue Types”1. Queue
Section titled “1. Queue”Queue is the most commonly used queue type in the queue module. It implements a standard first-in-first-out (FIFO) queue. Here is the basic usage of Queue:
Example
Section titled “Example”2. LifoQueue
Section titled “2. LifoQueue”LifoQueue is a last-in-first-out (LIFO) queue, similar to a stack. Here is the basic usage of LifoQueue:
Example
Section titled “Example”3. PriorityQueue
Section titled “3. PriorityQueue”PriorityQueue is a priority queue where elements are retrieved in order of priority. Here is the basic usage of PriorityQueue:
Example
Section titled “Example”Common Methods
Section titled “Common Methods”1. put(item, block=True, timeout=None)
Section titled “1. put(item, block=True, timeout=None)”Puts item into the queue. If block is True and the queue is full, wait for timeout seconds until the queue has free space. If timeout is None, wait indefinitely.
2. get(block=True, timeout=None)
Section titled “2. get(block=True, timeout=None)”Retrieves and removes an element from the queue. If block is True and the queue is empty, wait for timeout seconds until the queue has an element. If timeout is None, wait indefinitely.
3. qsize()
Section titled “3. qsize()”Returns the number of elements in the queue.
4. empty()
Section titled “4. empty()”Returns True if the queue is empty, otherwise False.
5. full()
Section titled “5. full()”Returns True if the queue is full, otherwise False.
Thread Safety
Section titled “Thread Safety”All queue types in the queue module are thread-safe, meaning multiple threads can safely operate on the same queue simultaneously without needing additional synchronization mechanisms. This makes the queue module an ideal choice for passing data in multi-threaded programming.
Example: Multi-Threaded Queue
Section titled “Example: Multi-Threaded Queue”Below is an example of using Queue to pass data between multiple threads:
Example
Section titled “Example”Common Attributes and Methods
Section titled “Common Attributes and Methods”Below is a tabular description of the common classes, methods, and attributes of the Python queue module (thread-safe queue), including descriptions and examples:
Core Classes of the queue Module
Section titled “Core Classes of the queue Module”| Class | Description | Use Case |
|---|---|---|
queue.Queue |
First-in-first-out (FIFO) queue | General task queue |
queue.LifoQueue |
Last-in-first-out (LIFO) queue (like a stack) | Scenarios requiring LIFO order |
queue.PriorityQueue |
Priority queue (min-heap implementation) | Processing tasks by priority |
queue.SimpleQueue |
Simpler FIFO queue (Python 3.7+) | Scenarios not needing advanced features |
Common Methods (supported by all queue classes)
Section titled “Common Methods (supported by all queue classes)”| Method | Description | Example | Return Value |
|---|---|---|---|
put(item) |
Put an element | q.put("task1") |
None |
get() |
Retrieve and remove an element | item = q.get() |
Queue element |
empty() |
Check if the queue is empty | if q.empty(): |
True/False |
full() |
Check if the queue is full | if q.full(): |
True/False |
qsize() |
Return the current size of the queue | size = q.qsize() |
Integer |
task_done() |
Mark a task as completed (used with join()) |
q.task_done() |
None |
join() |
Block until all tasks are completed | q.join() |
None |
Blocking Control Parameters
Section titled “Blocking Control Parameters”| Parameter | Description | Default | Example |
|---|---|---|---|
block |
Whether to block when the queue is empty/full | True |
q.get(block=False) |
timeout |
Blocking timeout duration (seconds) | None |
q.put(x, timeout=5) |
PriorityQueue-Specific Usage
Section titled “PriorityQueue-Specific Usage”Element format: (priority, data), lower priority values are dequeued first
Example
Section titled “Example”Examples
Section titled “Examples”Producer-consumer model:
Example
Section titled “Example”Priority task processing:
Example
Section titled “Example”Non-blocking retrieval (avoiding deadlocks):